新浪微博的OAuth2認證過程

1. 建立應用

在weibo.com上申請一個應用,獲取app key和app secret, 填寫redirect urijson

2. 獲取code

經過在瀏覽器訪問api

https://api.weibo.com/oauth2/authorize?client_id=xxxxx&redirect_uri=http%3A%2F%2Fwww.weibo.com&response_type=code&forcelogin=true

其中client_idredirect_uri是必須的,client_id是app key, redirect_uri是回調地址,都必須和應用所配置的相一致. 我這裏的redirect_uri填了http://www.weibo.com瀏覽器

在頁面中填入登陸的新浪微博帳號和密碼,按肯定後便會跳轉到 http://www.weibo.com?code=xxxxxxxxxx,這裏的參數code就是咱們所需的app

3. 獲取access token

這個過程須要經過post,能夠利用工具curl來模擬post請求curl

curl -d "client_id=xxx&client_secret=xxx&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fwww.weibo.com&code=xxxxx" "https://api.weibo.com/oauth2/access_token"

其中,client_secret是app secret,code是上一步獲取到的.工具

這一步的返回以下: {"access_token":"xxxx", ...} 這就是咱們所需的access token.post

(ps: 如果用curl模擬get,直接 curl "http://abc.com?p1=a&p2=b")url

4. 使用access token調用微博api的各個接口

這裏用"獲取本身所發微博"舉例,在瀏覽器打開調試

https://api.weibo.com/2/statuses/user_timeline.json?access_token=xxxx

並使用瀏覽器的調試工具查看返回結果,會自動對json進行格式化code


以上就是如何使用瀏覽器模擬微博的oauth2認證過程,省去使用sdk,能夠直接用 在咱們的應用中,可是,有一個小問題我想不明白,第二步獲取的code必須是經過 回調uri的參數處得到,不知道各sdk裏是如何獲取到這個code的?

相關文章
相關標籤/搜索