将 AppIdentityCredential 与 Google Drive API 一起使用
Posted
技术标签:
【中文标题】将 AppIdentityCredential 与 Google Drive API 一起使用【英文标题】:Using AppIdentityCredential with Google Drive API 【发布时间】:2012-10-10 15:47:00 【问题描述】:我正在使用 Google App Engine Identity 访问 Google Apps 用户的 Google Drive API。 我在 Google Apps 的管理 API 客户端访问中插入了 GAE URL,范围正确。
AppIdentityCredential credential = new AppIdentityCredential(Arrays.asList(DriveScopes.DRIVE_FILE));
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
我收到此错误:
com.google.appengine.repackaged.org.apache.http.impl.client.DefaultRequestDirector handleResponse
WARNING: Authentication error: Unable to respond to any of these challenges: authsub=WWW-Authenticate: AuthSub realm="https://www.google.com/accounts/AuthSubRequest" allowed-scopes="https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/docs,https://www.googleapis.com/auth/drive.file,https://www.googleapis.com/auth/drive.readonly,https://www.googleapis.com/auth/drive.metadata.readonly"
An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 OK
"code" : 401,
"errors" : [
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
],
"message" : "Invalid Credentials"
是否需要在 Google Apps 的 API 客户端访问中启用 GAE 应用程序?如果没有,我如何授权 GAE 应用访问 API Scope?
是否可以在本地/Eclipse 中测试 GAE 身份?
总结一下,有什么问题或不足?
【问题讨论】:
【参考方案1】:您无法在本地调试 AppEngine 凭据。调试的唯一方法是使用不同的凭据(用于生产的 AppEngine 和本地基于密钥的服务帐户)。它将需要为本地和生产环境编写不同的代码。
您需要将您的 appengine 服务帐户添加到 Team。如果您为项目使用自定义 Google Apps 域,则需要在自定义域中创建电子邮件组并添加该组。应将所有服务帐户添加到此组。
【讨论】:
第 1 点不正确。可以在本地开发应用引擎上进行测试。我一直这样做。 一年前不可能。 不仍然不可能。 developers.google.com/identity/protocols/… 注意:您只能在 Google App Engine 上运行的应用程序中使用 AppIdentityCredential 凭据对象【参考方案2】:考虑添加它尝试对其进行身份验证的范围。 我的代码中出现了同样的错误,最终通过添加 drive.file 范围来解决。
【讨论】:
【参考方案3】:两个重要步骤
-
获取应用程序默认凭据服务帐户 JSON 文件。
使用 JSON 文件的路径设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。示例:GOOGLE_APPLICATION_CREDENTIALS=c:/myFolder/my.jsonfile
仅供参考: https://developers.google.com/identity/protocols/application-default-credentials
【讨论】:
以上是关于将 AppIdentityCredential 与 Google Drive API 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
php [将产品与社交共享插件集成]将社交共享插件与WooCommerce集成 - Sharedaddy
php [将产品与社交共享插件集成]将社交共享插件与WooCommerce集成 - Sharedaddy
php [将产品与社交共享插件集成]将社交共享插件与WooCommerce集成 - Sharedaddy
php [将产品与社交共享插件集成]将社交共享插件与WooCommerce集成 - 分享此功能