新浪微博开放平台之OAuth2.0认证

1、先到开放平台创建一个移动应用。获得key和secret,接着到“应用信息”里面填写授权回调页的网址,该网址能够随意,可是必须是能訪问的。


2、通过新浪提供的api:https://api.weibo.com/oauth2/authorize去请求授权获得授权登录页面(网页形式)。然后填写你的新浪微博账号,提交。

參数:client_id即appkey。

redirect_uri即刚刚填写的授权回调页的网址

response_type为code

display为mobile,表示显示在移动设备上

state为authorize,可选

比如:

https://api.weibo.com/oauth2/authorize?client_id=65XXX7423&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code&display=mobile&state=authorize


3、账号和password提交后,会收到多次重定向的数据,但仅仅有前缀为回调页网址https://api.weibo.com/oauth2/default.html才包括我们要的code值。

比如。返回

https://api.weibo.com/oauth2/default.html?state=authorize&code=ea335c8922415641de2040abe5479139。当中的code的值将作为获取access token的API參数,这里的code是动态变化的。


4、通过新浪提供的api:https://api.weibo.com/oauth2/access_token获得token

这里的code參数就是刚刚重定向得到的。

比如:

https://api.weibo.com/oauth2/access_token?client_id=650627423&client_secret=65acaa05a0ef024071d0ede678e165fc&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code=11488678f95d1793f2dd94494b4b5dbe


5、将第四步返回的数据中keyword为“access_token”的值保存在本地。以后就能够用这个token去訪问各种api,获得各种数据。


6、这里有一份源代码。是我9个月前写的。能够执行。只是,刚刚測试了一下。由于官方api有更改,导致无法获取主页微博数据。

參考官方文档,改一下api的參数应该就又能够顺利执行下去了。

http://download.csdn.net/detail/awaylin113/7780505


原文地址:https://www.cnblogs.com/yfceshi/p/6899785.html