2015-02-11 203 views
0

该类: com.google.api.client.googleapis.auth.oauth2.GoogleCredential 创建实例时需要certifcate file as a parameter设置谷歌证书的证书文件,不使用文件

我使用泽西在谷歌应用程序引擎,如果我打开我的文件(这是在资源中)使用番石榴资源一切工作正常本地,但部署到应用程序引擎我得到URI is not Hierarchical error

所以我应该使用的getResourceAsStream和convert to temporary file ...

但谷歌应用程序引擎限制FileOutputStream中的使用(它不允许使用)。

有什么办法来从一个InputStream一个临时文件,而无需使用FileOutputStream中...或者是任何人都familar与使用谷歌API的OAuth

+0

无论您采用何种方式,您都无法在App Engine上创建文件。您可能想使用Blobstore来保存类似的内容,或者将文件保存到GCS本身。由于App Engine可以(而且会)将您的应用程序从实例移动到实例,因此将应用程序触及文件系统会变得不安全并且非常复杂 – Patrice 2015-02-13 19:32:20

回答

0

正确的方式做,这在部署应用程序的不同方式发动机:

Collection<String> scopes = new ArrayList<>(1); 
scopes.add("https://www.googleapis.com/auth/bigquery"); 
AppIdentityCredential credential = new AppIdentityCredential(scopes); 
Bigquery bigquery = new Bigquery.Builder(new NetHttpTransport(), new GsonFactory(), credential).setApplicationName("myAppName").build(); 

这是方式不同的推荐本地开发/测试时 - 即依赖于文件引用(所以你必须进行测试/生产不同的代码):

String account = properties.get("oauthAccount"); 
String clientId = properties.get("oauthClientId"); 
String secret = properties.get("oauthSecret"); 
File privateKey = getCertficateFile(); 
GoogleCredential.Builder builder = new GoogleCredential.Builder(); 
builder.setTransport(new NetHttpTransport()); 
builder.setServiceAccountId(account); 
builder.setJsonFactory(new GsonFactory()); 
builder.setClientSecrets(clientId, secret);    
builder.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/bigquery")); 
builder.setServiceAccountPrivateKeyFromP12File(privateKey); 
GoogleCredential googleBigQueryCredential = builder.build(); 
Bigquery bigquery = new Bigquery.Builder(new NetHttpTransport(), new GsonFactory(), googleBigQueryCredential).setApplicationName("myAppName")                              .build();