基于Java的Google App Engine和用于服务帐户的Google Drive API

时间:2015-11-12 22:00:43

标签: java google-app-engine google-drive-api

我在Google App Engine Framework中有关于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();


}

1 个答案:

答案 0 :(得分:2)

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上的

。如描述here,您将在AppEngine中使用UrlFetchTransport。看起来应该是这样的:

httpTransport = new UrlFetchTransport.Builder().build();
顺便说一下。您应该从appengine日志中添加错误,因为这样可以更容易地诊断代码。