利用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

后面的处理基本一样,就不多啰嗦了

源代码:为什么不能上传源代码

原文地址:https://www.cnblogs.com/cnsnet/p/2484861.html