Google drive api 谷歌appengine(java)上的服务帐户

Google drive api 谷歌appengine(java)上的服务帐户,google-drive-api,Google Drive Api,我们如何使用Google Drive API设置服务帐户授权,以便代表域内的其他用户行事 代码在第201行抛出HTTP错误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.porta

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

代码在第201行抛出HTTP错误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)
}

引发错误的行是: .setServiceAccountPrivateKeyFromp12文件(新的java.io.File(keyURL))

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

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


欢迎提供任何帮助:-)

将resources文件夹从src/main/resources移动到src/resources,并将服务\u帐户\u PKCS12\u文件\u路径更改为“/resources/xxx privatekey.p12”。现在它就像一个符咒

  /** 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;