Google appengine上的服务帐户(java)

时间:2014-05-28 14:50:32

标签: google-drive-api

我们如何使用Google Drive API设置服务帐户授权,以便我们代表域内的其他用户行事。

代码在第201行抛出HTTP ERROR 500,请参阅下面的stacktrace:

URI is not hierarchical

Caused by:

java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.<init>(File.java:418)
    at com.caase.portal.DriveServlet.getDriveService(DriveServlet.java:201)
    at com.caase.portal.DriveServlet.doGet(DriveServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet.service(AbstractAuthorizationCodeServlet.java:130)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

请参阅下面提到的代码:

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

  /** Path to the Service Account's Private Key file */
  private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "xxx-privatekey.p12";

  /**
   * 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, URISyntaxException {

  URI keyURL;
  keyURL = DriveServlet.class.getResource(SERVICE_ACCOUNT_PKCS12_FILE_PATH).toURI();



HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
    .setServiceAccountScopes(DriveScopes.all())
    .setServiceAccountUser(userEmail)
    .setServiceAccountPrivateKeyFromP12File(new java.io.File(keyURL))
    .build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
    .setHttpRequestInitializer(credential).build();
return service;

}

抛出错误的行是:     .setServiceAccountPrivateKeyFromP12File(new java.io.File(keyURL))

如果我拆分该行,则在新文件创建中会发生错误:     new java.io.File(keyURL)

密钥文件位于:src / main / resources

欢迎任何帮助: - )

1 个答案:

答案 0 :(得分:0)

将resources文件夹从src / main / resources移动到src / resources,并将SERVICE_ACCOUNT_PKCS12_FILE_PATH更改为&#39; /resources/xxx-privatekey.p12'。它现在就像一个魅力!