2011-10-01 51 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

相關問題