【问题标题】:Getting user profile with google oauth2使用 google oauth2 获取用户个人资料
【发布时间】:2011-10-01 20:52:02
【问题描述】:

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

来自developers guide 的“检索配置文件”部分:

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。

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

【问题讨论】:

    标签: oauth-2.0 user-profile


    【解决方案1】:

    您要查找的范围是:

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

    这个问题已经回答here

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的回答。在我的情况下,我完全迷失在谷歌关于 oauth、oauth2、opensocial 等的大量 api 文档中。我设法挖了出来,发现我所需要的只是对代码进行一些小的修改并提供正确的范围。对于使用 oauth2 的 google 个人资料,我需要添加 googleapis.com/auth/userinfo.profile scope
      【解决方案3】:

      也许有点晚了,但这对某人有帮助吗?以下是我为获取 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(); not working I'm lost

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 本文与 Google API 无关
        猜你喜欢
        • 1970-01-01
        • 2018-08-25
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 2016-07-29
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        相关资源
        最近更新 更多