1. 創(chuàng)建應(yīng)用
在weibo.com上申請(qǐng)一個(gè)應(yīng)用,獲取app key和app secret, 填寫(xiě)redirect uri
2. 獲取code
通過(guò)在瀏覽器訪問(wèn)
https://api.weibo.com/oauth2/authorize?client_id=xxxxx&redirect_uri=http%3A%2F%2Fwww.weibo.com&response_type=code&forcelogin=true
其中client_id和redirect_uri是必須的,client_id是app key, redirect_uri是回調(diào)地址,都必須和應(yīng)用所配置的相一致. 我這里的redirect_uri填了http://www.weibo.com
在頁(yè)面中填入登錄的新浪微博賬號(hào)和密碼,按確定后便會(huì)跳轉(zhuǎn)到 http://www.weibo.com?code=xxxxxxxxxx,這里的參數(shù)code就是我們所需的
3. 獲取access token
這個(gè)過(guò)程需要通過(guò)post,可以利用工具curl來(lái)模擬post請(qǐng)求
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.
(ps: 若是用curl模擬get,直接 curl "http://abc.com?p1=a&p2=b")
4. 使用access token調(diào)用微博api的各個(gè)接口
這里用"獲取自己所發(fā)微博"舉例,在瀏覽器打開(kāi)
https://api.weibo.com/2/statuses/user_timeline.json?access_token=xxxx
并使用瀏覽器的調(diào)試工具查看返回結(jié)果,會(huì)自動(dòng)對(duì)json進(jìn)行格式化
以上就是如何使用瀏覽器模擬微博的oauth2認(rèn)證過(guò)程,省去使用sdk,可以直接用 在我們的應(yīng)用中,但是,有一個(gè)小問(wèn)題我想不明白,第二步獲取的code必須是通過(guò) 回調(diào)uri的參數(shù)處獲得,不知道各sdk里是如何獲取到這個(gè)code的?