Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于服务帐户的基于Java的Google应用程序引擎和Google驱动API_Java_Google App Engine_Google Drive Api - Fatal编程技术网

用于服务帐户的基于Java的Google应用程序引擎和Google驱动API

用于服务帐户的基于Java的Google应用程序引擎和Google驱动API,java,google-app-engine,google-drive-api,Java,Google App Engine,Google Drive Api,我在googleappengine框架中有以下关于Eclipse的程序,它正在使用googledriveapi。新文件在Google Drive中创建。我有一个文件夹ID,在服务帐户和我的个人Gmail帐户之间共享。当我在eclipse上运行程序时,我可以看到在GoogleDrive上生成的文件 但是,当我在GAE上部署相同的程序时,它无法在Google Drive上创建文件。任何有帮助的指针 public class GDrive implements Callable<String&g

我在googleappengine框架中有以下关于Eclipse的程序,它正在使用googledriveapi。新文件在Google Drive中创建。我有一个文件夹ID,在服务帐户和我的个人Gmail帐户之间共享。当我在eclipse上运行程序时,我可以看到在GoogleDrive上生成的文件

但是,当我在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();


}
public类GDrive实现了可调用{
专用静态HttpTransport-HttpTransport;
私有静态字符串应用程序\u NAME=“xxxxxxx”;
私有静态字符串用户\u电子邮件=”xxxxxxx@gmail.com";
私有静态JacksonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
私有静态字符串服务\u帐户\u电子邮件=“帐户-1@xxxxxxx.iam.gserviceaccount.com";
私有静态字符串客户端\u ID=“xx06238724813717381290”;
私有静态字符串密钥\u PASSWORD=“notasecret”;
私有静态字符串客户端\u SECRET\u P12\u LOC=“file.P12”;
MyConstants obMyConstants=新的MyConstants();
public void insertLog(RootLog obRootLog)抛出IOException、GeneralSecurityException{
字符串LogFolderId=obLBLSSS_Constants.LogFolderId;
//Google Drive中父文件夹的ID
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential=新建GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_工厂)
.setServiceAccountId(服务\帐户\电子邮件)
.setClientSecrets(客户端ID、密钥密码)
.setServiceAccountScopes(DriveScopes.all())
.SetServiceAccountPrivateKeyfromP12文件(
新的java.io.File(CLIENT\u SECRET\u P12\u LOC))
.build();
Drive driveService=new Drive.Builder(httpTransport、JSON\u工厂、凭证)
.setApplicationName(应用程序名称).build();
Gson Gson=new GsonBuilder().create();
字符串eJsonOutput=gson.toJson(obRootLog);
Instant-Instant=Instant.now();
字符串文件名=instant+“”+obRootLog.requestURI;
//文件的元数据。
File child=新文件();
setTitle(文件名);
setDescription(“我的文件描述”);
setMimeType(“application/json”);
child.setParents(
Arrays.asList(newparentreference().setId(LogFolderId));
//文件的内容。
java.io.File fileContent=新的java.io.File(文件名);
PrintWriter writer=新的PrintWriter(文件内容,“UTF-8”);
writer.println(eJsonOutput);
writer.close();
FileContent mediaContent=新的FileContent(“应用程序/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日志中添加错误,因为这样更容易诊断代码