gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:28:09 25 4
gpt4 key购买 nike

我正在编写一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com