新浪微博开放平台之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
總結
以上是生活随笔為你收集整理的新浪微博开放平台之OAuth2.0认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言 条件编译(if )
- 下一篇: 一个完整的mybatis项目,包含增删改