是否可以使用 Google Cloud Endpoints 内置身份验证与 Google+ 域 API?

Is it possible to use Google Cloud Endpoints built in authentication with Google+ Domains API?

Google Cloud Endpoints 有它自己的身份验证过程,在该过程中后端端点方法只是传递一个 com.google.appengine.api.users.User 对象。

https://cloud.google.com/appengine/docs/java/endpoints/auth

Google+ 域 API 指定了自己的身份验证过程,以便获得 com.google.api.client.auth.oauth2.Credential 对象。这允许构建 com.google.api.services.plusDomains.PlusDomain 对象。

https://developers.google.com/+/domains/authentication/

您将如何整合这两个身份验证过程?这是针对 网络应用程序 (Java 脚本)和 Google App Engine (Java) 后端。

在理想情况下,我希望能够在用户离线时通过我的 JS 应用检索用户 bio/profile 基本信息。

用例:我有一个评论线程,其中每个评论都有一个作者作为 com.google.appengine.api.users.User 对象保存在 Google 数据存储中。但是,当我在我的 JS 网络应用程序中呈现评论线程时,我想显示每个作者的个人资料图片。如果我可以从 Web 应用程序调用以检索每个评论者的简历,我可以为后端节省大量工作。 Web 应用程序会将用户对象设置为 JSON。其中包括用户 ID 和电子邮件。

因此,您的用例是:

  • 您的用户对您的应用进行身份验证,授予在您的端点中正确接收 com.google.appengine.api.users.User 对象所需的基本 userinfo.profile 范围 API
  • 您将这些用户对象保存到数据库中,当您检索它们以显示他们评论的线程时,您想要调用 google+ API people.get method 检索他们的头像图像 URL

解决方案:如果您的用户看到一个 oauth 流程,让他们在完成该流程后使用 Google API 客户端库授予 the scope required for the google+ API call (the profile scope) in addition to the regular endpoints "userinfo.profile" scope, it should be no problem to call the Google+ API, either from the JS client or from the Java 后端获取凭据。

为了避免每次都重新验证它们,您应该 serialize and store a credentials object from the language in question,或者您甚至可以简单地跟踪它们授予的刷新令牌并通过低级 OAuth 舞蹈来获得一个新的访问令牌(你可能想要做前者,因为它会为你做这件事)。

如前所述,elsewhere on the web (in several other places as well), the userid from the User object 与 Google+ 配置文件 ID 不同,因此在使用端点方法参数用户对象时请注意这一点。因此,您将无法使用 User 对象中的用户 ID 来调用 people.get

相反,您应该在用户首次 signed-in 或至少通过授予必要 Google 的 oauth 流程时存储用户的 Google+ 配置文件 ID + 范围,以及您已经使用的用户对象。一旦从每个用户的 Google+ 个人资料 ID 中检索到 Google+ 个人资料 ID,您将必须使用(反)序列化凭证对象或 refresh/access 令牌来调用 Google+ API存储中的数据模型(无论您使用何种解决方案,从 Datastore 到 SQL,等等)