[英]Java based Google App Engine and Google Drive API for Service Accounts
我在Google App Engine框架中的Eclipse上具有以下程序,並且該程序可與Google Drive API一起使用。 在Google雲端硬盤中創建新文件。 我有一個文件夾ID-正在服務帳戶和我的個人Gmail帳戶之間共享。 當我在Eclipse上運行該程序時,我可以看到在Google驅動器上生成的文件。
但是,當我在GAE上部署相同的程序時,它無法在Google雲端硬盤上創建文件。 任何可以幫助的指針。
public class GDrive implements Callable<String> {
private static HttpTransport httpTransport;
private static String APPLICATION_NAME = "xxxxxxx";
private static String USER_EMAIL = "xxxxxxx@gmail.com";
private static JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static String SERVICE_ACCOUNT_EMAIL = "account-1@xxxxxxx.iam.gserviceaccount.com";
private static String CLIENT_ID = "xx06238724813717381290";
private static String KEY_PASSWORD = "notasecret";
private static String CLIENT_SECRET_P12_LOC = "file.p12";
MyConstants obMyConstants = new MyConstants();
public void insertLog(RootLog obRootLog) throws IOException, GeneralSecurityException {
String LogFolderId = obLBLSSS_Constants.LogFolderId;
//ID of the Parent Folder in Google Drive
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setClientSecrets(CLIENT_ID, KEY_PASSWORD)
.setServiceAccountScopes(DriveScopes.all())
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(CLIENT_SECRET_P12_LOC))
.build();
Drive driveService = new Drive.Builder(httpTransport, JSON_FACTORY,credential)
.setApplicationName(APPLICATION_NAME).build();
Gson gson = new GsonBuilder().create();
String eJsonOutput = gson.toJson(obRootLog);
Instant instant = Instant.now();
String filename = instant + "_" + obRootLog.requestURI;
// File's metadata.
File child = new File();
child.setTitle(filename);
child.setDescription("My File Description");
child.setMimeType("application/json");
child.setParents(
Arrays.asList(new ParentReference().setId(LogFolderId)));
// File's content.
java.io.File fileContent = new java.io.File(filename);
PrintWriter writer = new PrintWriter(fileContent, "UTF-8");
writer.println(eJsonOutput);
writer.close();
FileContent mediaContent = new FileContent("application/json", fileContent);
File filetemp = driveService.files().insert(child, mediaContent).execute();
}
GAE文件系統是只讀的,因此您無法像嘗試使用該文件一樣寫入文件
java.io.File fileContent = new java.io.File(filename);
PrintWriter writer = new PrintWriter(fileContent, "UTF-8");
writer.println(eJsonOutput);
writer.close();
為什么不將json數據放在字節數組中,並用ByteArrayInputStream
包裝。 像這樣:
InputStream bais = new ByteArrayInputStream(gson.toJson(obRootLog).getBytes());
然后將輸入流用作您的參數
FileContent mediaContent = new InputStreamContent("application/json", bais);
呼叫。
此外,您不能使用
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
在App Engine上。 如此處所述,您可以在AppEngine中使用UrlFetchTransport。 它看起來應該像這樣:
httpTransport = new UrlFetchTransport.Builder().build();
順便說說。 您應該從Appengine日志中添加錯誤,因為這樣可以更輕松地診斷代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.