2015-02-11 200 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();