google-app-engine - 使用 Google Cloud Endpoint (Java) 的内置身份验证时,如何获取当前用户的 Google+ 个人资料?

标签 google-app-engine google-plus google-cloud-endpoints

我的设置

后端:带有 Google Cloud Endpoints 的 Google App Engine (Java) 使用 Endpoint 的内置身份验证

前端:AngularJS 网络应用

问题

我需要为我的用户获取 Google+ 个人资料。关键字“我”通常可用于获取当前用户的 Google+ 个人资料,但是由于 Google Cloud Endpoints 中的所有身份验证都是在幕后完成的,所以我无论如何都看不到获取凭据或 token 当前用户。所有你得到的 com.google.appengine.api.users.User 对象。

在使用 Google Cloud Endpoint 的内置身份验证时,有没有办法获取用户凭据或访问 token ?

注意:Google+ 个人资料 ID 不同于 Google 帐户 ID。

可能的解决方案

我可以只使用带有关键字“me”的 Google+ JS 客户端,让用户发送他们的 Google+ ID,然后存储它并将其绑定(bind)到他们的 Google 帐户 ID,但这将是非常不安全的,因为用户可以破解他们发送任何 Google+ 帐户 ID 的方式。

最佳答案

使用 Google Cloud Endpoint 的内置身份验证时可以获得用户访问 token 。

  1. 将参数 HttpServletRequest 请求 添加到您的 Google Cloud 端点,如下所示。这将允许您获得原始请求。

  2. 然后您需要检索名为Authentication 的 header 。这将获得一个 Bearer 访问 token ,允许您构建凭据来模拟经过身份验证的用户。

  3. 接下来,您将使用该 Bearer 访问 token 构建一个 com.google.api.client.googleapis.auth.oauth2.GoogleCredential 对象。您将需要它来构建 Plus 服务。

  4. 使用 Plus 构建器使用您刚创建的凭据构建一个 Plus 服务对象。

示例代码

@ApiMethod(path = "myPath")
public void myEndpoint(HttpServletRequest request, ParmOne paramOne, ...) throws OAuthRequestException {

    if (user == null) {
        throw new OAuthRequestException("Authentication error!");
    }

    GoogleCredential credentialAsUser = new GoogleCredential().setAccessToken(request.getHeader("Authorization").substring(7)); // Start string at index position 7 to remove prefix "Bearer" from token.

    Plus plus = new Plus.Builder(new UrlFetchTransport(), new JacksonFactory(), credentialAsUser).setApplicationName("my-app").build();
    Person profile = plus.people().get("me").execute();

}

文档

可以找到 Google Plus 客户端的 Java 文档 here .

可以在 Java 文档中找到有关创建 Google 凭据的说明 here .

关于google-app-engine - 使用 Google Cloud Endpoint (Java) 的内置身份验证时,如何获取当前用户的 Google+ 个人资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942681/

相关文章:

java - NoSuchMethodError : com. google.common.base.Platform.systemNanoTime() 在 GWT 项目中

YouTube API - 列出我管理的所有 channel

mysql - 在 Google App Engine 开发环境中使用 JPA 时,非 Ascii 字符保存为 '?'

objective-c - oAuth2 在 iOS7 中检索个人资料电子邮件

python - 当我使用 App Engine SDK 在本地主机中部署我的谷歌云端点 python 应用程序时出错

java - 从 Google App Engine 后端检索实体到 Android 应用程序

python - 如何使用 Google App Engine Python 将变量从 app.yaml 传递到 main.py

mysql - 从 App Engine 连接到 Google Cloud SQL 时出现 ENOENT

java - 如果服务器收到 0 作为端口号,如何中继 NAT 动态端口号?

iOS 谷歌登录 : Get list of people from Google+ including their names and profile pictures