利用Google開發接口獲取Google用戶信息,OAuth2.0,profiles


直奔主題,利用Google OAuth2.0獲取用戶信息.希望對大家有幫助.

獲取方法有好多種,網上有的方法是通過讀取Google contacts中返回信息中的用戶信息,貌似只能讀取用戶名和Email,我這個是直接用OAuth2.0來獲取的.

這個是接口文檔地址:https://developers.google.com/accounts/docs/OAuth2Login

首先,到https://code.google.com/apis/console#access 建立一個應用,申請成功后,你將獲得Client ID和Client secret等信息,這個在后面會用到.

接下來就是訪問Google的接口了.

  訪問地址:https://accounts.google.com/o/oauth2/auth

  接口參數:Google公布的參數有5個,其中有四個是必須的.

      response_type:code or token,只能是這兩個中的一個,

      client_id:就是上面申請的Client ID

      redirect_uri:Google處理完申請后返回地址,用於接收參數,處理后面的事情.

      scope:這個是要填寫你想要獲取哪些信息,在本例中,我們需要傳給Google的是

          https://www.googleapis.com/auth/userinfo.profilehttps://www.googleapis.com/auth/userinfo.email

      state:可選參數,隨便填寫,看有什么需要就自己寫上,比如處理完數據后要跳轉到某個頁面,就可以寫到這個參數里.

  通過上述表述,我們構造出添加了參數后的訪問地址: https://accounts.google.com/o/oauth2/auth?client_id=1079550655222.apps.googleusercontent.com&response_type=token&state=/default.aspx&redirect_uri=http%3a%2f%2flocalhost:4800%2fgoogleauth.aspx&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile

訪問以后就會跳轉到Google的登陸頁面,填寫完登陸信息以后,會提示你是否授權改應用訪問你的Profile

點擊允許后,Google就會返回一個token,然后我們再使用這個token來獲取用戶的Profile信息了.

然后我們再構建一個訪問

  接口地址:https://www.googleapis.com/oauth2/v1/userinfo

  接口參數:access_token:就是上面獲取的token

  構建好的地址是:https://www.googleapis.com/oauth2/v1/userinfo?access_token=AAA

然后我們就能獲取到用戶的信息了.

獲取一點信息,居然經歷過這么多的步驟,可謂是跋山涉水了.其實我們可以用一種懶人的辦法,在返回token的時候通過WebRequest對象來代替我們做第二次訪問.

 1 Dim token As String = Request.QueryString("token")
 2 
 3 If token IsNot Nothing AndAlso String.IsNullOrEmpty(token) Then
 4     Dim urlData As String = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" & token
 5     Dim wReq As WebRequest = WebRequest.Create(urlData)
 6     Dim responseText As String = New StreamReader(wReq.GetResponse().GetResponseStream()).ReadToEnd()
 7 
 8     Dim js As New JavaScriptSerializer()
 9     Dim userInfo As Dictionary(Of String, String) = js.Deserialize(Of Dictionary(Of String, String))(responseText)
10 
11     For Each item As KeyValuePair(Of String, String) In userInfo
12         Response.Write(item.Key & ":" & item.Value & "<br />")
13     Next
14 End If

在這里,我用了System.Web.Script.Serialization命名空間中的JavaScriptSerializer來反序列化獲得的JSON到一個userInfo變量中.

還有一點要注意,由於第一次訪問獲取token的時候,返回的地址並不是類似於http://www.cnblogs.cn/g.aspx?access_token=AAA 這種格式,而是http://www.cnblogs.cn/g.aspx#access_token=AAA的.所以我們還要進行一個小的處理,在接收頁面g.aspx中添加如下js代碼

 

<script type="text/javascript">
    var params = {}, queryString = location.hash.substring(1);
    window.location.href = "/googleauth.aspx?" + queryString;
</script>

 

這樣就好了.

還有一種方法也可以獲取到用戶的信息,並且也不需要申請app,不需要Client ID.不過Google好像要停止對這種接口的支持,不建議大家使用

接口文檔地址:https://developers.google.com/accounts/docs/OAuth

構造訪問地址:https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Flocalhost:50475%2fgoogleauth.aspx&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile

后面的處理基本一樣,就不多啰嗦了

源代碼:為什么不能上傳源代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM