java - 服务帐户在本地工作但在应用引擎上部署时中断的 Google Drive Auth

标签 java google-app-engine google-drive-api google-oauth

我正在编写一个 App Engine 网络应用程序,用于从 Google Drive 上的文件夹加载图片库。该代码在本地运行时工作正常(图像被检索并正确显示在网页上),但在部署到 Google App Engine 时它​​会中断。具体来说,启动如下方法时,返回的Drive服务为null,没有抛出异常。凭据(SERVICE_ACCOUNT_EMAIL 和 PKCS12 文件)应该是正确的,因为它们在本地运行代码时有效。我在这里错过了什么?提前致谢。

public static Drive getDriveService() {
    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;
}

最佳答案

实际上有两种方法可以通过 Drive Api 对服务帐户进行身份验证。如果您使用 Dev App Server 在本地计算机上进行调试,那么您使用的那个是可以的,但是一旦部署到 GAE 就无法工作。 您可以使用此代码段进行身份验证,该代码段取自

上的 Drive API 引用

https://developers.google.com/drive/service-accounts#google_app_engine_project_service_accounts

只需将您从控制台获取的 API Key 放入代码中即可。这是我为 Drive API 的 GAE 身份验证找到的唯一有效解决方案。

这是您要添加到项目中的代码:

import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
...

/** The API Key of the project */
private static final String API_KEY = "the_api_key_of_the_project";

/**
 * Build and returns a Drive service object authorized with the
 * application's service accounts.
 *
 * @return Drive service object that is ready to make requests.
 */
public static Drive getDriveService() throws GeneralSecurityException,
    IOException, URISyntaxException {
  HttpTransport httpTransport = new NetHttpTransport();
  JsonFactory jsonFactory = new JacksonFactory();
  AppIdentityCredential credential =
      new AppIdentityCredential.Builder(DriveScopes.DRIVE).build();
  GoogleClientRequestInitializer keyInitializer =
      new CommonGoogleClientRequestInitializer(API_KEY);
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential)
      .setGoogleClientRequestInitializer(keyInitializer)
      .build();
  return service;
}

关于java - 服务帐户在本地工作但在应用引擎上部署时中断的 Google Drive Auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999922/

相关文章:

python - 在 Google App Engine 上使用异步 urlfetch 启动后端

python - 在 AppEngine Python 上使用 Reportlab 生成的 PDF 文档中添加图像文件的正确方法

google-drive-api - Google Drive 文件夹删除行为

javascript - 从匿名用户使用javascript将文件上传到Google驱动器

android - 为 Android 应用程序同步数据库

Java `final` 方法 : what does it promise?

java - 如何忽略字段级别的未知字段?

java - 如何禁用 "this page is tracking your location"

java - 没有 SQL 服务器的数据库持久性

xml - Gmail 上下文小工具 - 更新的创建方式是什么?