3

我正在尝试在使用 google-oauth2 登录时获取用户个人资料信息。用户成功登录,我可以获得 access_token 并可以在需要时刷新令牌。尽管阅读了文档并尝试了几个小时,但我无法获得有关用户的任何信息。

从开发人员指南的“检索配置文件”部分:

https://www.google.com/m8/feeds/profiles/domain/domainName/full

应该足够了。我尝试将“gmail.com”、“google.com”、“gmail”、“google”、“orkut”、“orkut.com”、myregisteredappsdomainname(和 .com)作为域名。我也试过了

https://www.google.com/m8/feeds/profiles/domain/domainName/full?access_token=access_token_for_user

我设法得到的只是401错误,它说“这是一个错误。”。关于 401 错误,我刷新了令牌并再次尝试使用新令牌,但一直收到 401。

登录后如何获取用户的个人资料和图片地址?

4

4 回答 4

2

您正在寻找的范围是:

https://www.googleapis.com/oauth2/v1/userinfo

这已经在这里回答了

于 2012-03-21T10:24:38.847 回答
1

即使在正确定义范围和获取访问令牌等之后,我在请求配置文件时也遇到了类似的错误。对我来说,诀窍是在我的请求中包含 API 版本。请参阅此处了解更多信息http://code.google.com/googleapps/domain/profiles/developers_guide.html#Versioning

于 2011-10-25T02:29:47.000 回答
0

也许有点晚了,但这可能对某人有帮助。以下是我为获取 gplus 用户配置文件而编写的工作代码

在 HTML 下面的标记将显示 goolge 登录按钮

<span id="signinButton">
    <span
      class="g-signin"
      data-callback="signinCallback"
      data-clientid="YOUR GPLUS CLIENT ID"
      data-cookiepolicy="single_host_origin"
      data-scope="email">
    </span>
</span>   

下面是java脚本

 var access_token;
/**
 * Called when the Google+ client library reports authorization status.
 */
function signinCallback(authResult) {
    access_token = authResult.access_token;

    gapi.client.load('plus', 'v1', function () {
        gapi.client.plus.people.get({ userId: 'me' }).execute(printProfile);
    });
}

/**
 * Response callback for when the API client receives a response.
 *
 * @param resp The API response object with the user email and profile information.
 */
function printProfile(resp) {
    if (resp.code != 403) {
     console.log('name:' + access_token.givenname);
     console.log('last name:' + access_token.lastname);
     console.log('email:' + access_token.emails[0]);
     console.log('gender:' + access_token.gender);
     console.log('profile image url:' + access_token.image.url);
    }
}

请确保您在 body 标记中异步加载 google api javascript,如下所示

            <script type="text/javascript">
                (function () {
                    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                    po.src = 'https://apis.google.com/js/platform.js';
                    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                })();
</script>

要处理注销,请参阅我在下面的链接中提供的答案,您需要将 access_token 存储在后端,以便在注销调用期间使用它,在我的情况下,我已经存储在会话中并通过 ajax 调用 gapi.auth.signOut( ); 不工作我迷路了

于 2014-08-06T07:16:49.460 回答
-1

嘿,你为什么不看看给出的代码:http: //www.codeproject.com/KB/aspnet/OAuth4Client.aspx

它绝对可以帮助你。该项目实际上是一个 oauth 游乐场,用于将正确的 oauth 标头发送到正确的端点。

于 2012-01-16T10:29:02.977 回答