2011-10-01 56 views
3

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

developers guide “检索个人资料” 部分:

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

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

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

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

如何在登录时为用户获取配置文件信息和图像地址?

回答

1

即使在正确定义范围并获取访问令牌等之后,我也会收到类似的错误请求配置文件。我的技巧是在我的请求中包含API版本。在这里看到更多的信息http://code.google.com/googleapps/domain/profiles/developers_guide.html#Versioning

+2

谢谢为答案。在我的情况下,我完全失去了大量有关oauth,oauth2,opensocial等的Google api文档。我设法挖掘出我的出路,并发现我需要的只是代码中的一些小修改并提供了正确的范围。对于具有oauth2的Google配置文件,我需要添加https://www.googleapis.com/auth/userinfo.profile范围 – hinoglu

2

你正在寻找的范围是:

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

这已经已经回答here

+0

现在范围仅仅是'''profile'''。 – Supertecnoboff

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); 
    } 
} 

请确保您加载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> 

T o处理注销参考我在下面提供的答案链接,你需要将access_token存储在后端,以便在注销时调用这个用于在我的情况下,我已经存储在会话中并通过ajax调用 gapi.auth.signOut(); not working I'm lost

相关问题