Google 凭据服务帐户

Google Credential Service Account

我无法使用生成的服务帐户连接到驱动器 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;
}

}

经过大量搜索,我发现在调用末尾添加的另一种方法解决了我的问题,它是“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;
}