Google凭据服务帐户

时间:2015-02-26 19:14:21

标签: java google-api google-drive-api google-oauth

我无法使用生成的服务帐户连接到Drive API。我已按照此页link的说明列表进行操作。但是,当我运行我的代码时,我将文件上传到我的驱动器时会出现空指针异常。我不确定我在这里做错了什么。我的目标是创建一个命令行应用程序,我可以用它将文件上传到我的驱动器而无需任何用户交互。我的代码在

之下
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.*;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;



public class DomainAuth {

/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "972752586307-ecmn7ckdt2o1s3h37canak97d9pa11b7@developer.gserviceaccount.com";

/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "mykey2.p12";
public static void main(String[] args) {

    try {
        Drive service = getDriveService(SERVICE_ACCOUNT_EMAIL);

        //Insert a file  
        File body = new File();
        body.setTitle("My document3");
        body.setDescription("A test document");
        body.setMimeType("text/plain");

        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());

    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}



/**
 * Build and returns a Drive service object authorized with the service accounts
 * that act on behalf of the given user.
 *
 * @param userEmail The email of the user.
 * @return Drive service object that is ready to make requests.
 */
public static Drive getDriveService(String userEmail) throws GeneralSecurityException,
    IOException {

    java.io.File key = new java.io.File("key.p12");
    System.out.println(key.getAbsolutePath());
  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE))
      .setServiceAccountUser(userEmail)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}

}

1 个答案:

答案 0 :(得分:2)

经过多次搜索后,我发现在通话结束时添加了另一种方法解决了我的问题,它是" setApplicationName(" MyAppName") "

    public static Drive getDriveService2() {
    HttpTransport httpTransport = new NetHttpTransport();
    GsonFactory jsonFactory = new GsonFactory();
    GoogleCredential credential;
    Drive service = null;
    List<String> scopes = Arrays.asList(DriveScopes.DRIVE);
    try {
        credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(scopes)
                .setServiceAccountPrivateKeyFromP12File(
                        new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
                .build();
        service = new Drive.Builder(httpTransport, jsonFactory, null).setApplicationName("MyAppName")
                .setHttpRequestInitializer(credential).build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return service;
}