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

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

我在 Google App Engine 框架中的 Eclipse 上有以下程序,它正在与 Google Drive API 配合使用。新文件将在 Google 云端硬盘中创建。我有一个文件夹 ID - 在服务帐户和我的个人 Gmail 帐户之间共享。当我在 Eclipse 上运行该程序时,我可以看到在 google 驱动器上生成的文件。

但是,当我在 GAE 上部署相同的程序时,它无法在 Google Drive 上创建文件。任何有帮助的指示。

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 上。如上所述here您将在 AppEngine 中使用 UrlFetchTransport。它应该看起来像这样:

httpTransport = new UrlFetchTransport.Builder().build();

顺便说一下。您应该添加应用程序引擎日志中的错误,因为这样可以更轻松地诊断您的代码。

关于用于服务帐户的基于 Java 的 Google App Engine 和 Google Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33682256/

相关文章:

java - 下载 Java 网络源代码

java - Spring @JsonIgnore 不起作用

Java可执行jar类路径混淆

mysql - 在 Google Appengine 上接收 SQLNonTransientConnectionException

python - 从哪里开始编写用于将本地目录同步到 Google Drive 的 Python 脚本?

java - 使用图形和 Java OOP 的类似于 Tic Tac Toe 的游戏的问题

java - AppEngine - Java - 从 Web 界面插入对象

google-app-engine - 为什么 Google Cloud DataStore 不支持针对不同属性的多个不等式过滤器?

javascript - Google Drive Rest API

google-apps-script - 在其他文档上设置属性