為了吸納用戶、提高網站的用戶體驗性,現在很多網站都采取第三方登陸,比較常用的就是微信、QQ、微博了,接下來我來分享一下微博登陸接入的過程:
開發文檔:http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6
開發步驟:
1、通過新浪微博的開放平台去注冊一個應用。
之后你會得到一個App Key和一個App Secret。擁有它們,你才可以申請權限。
2、在高級信息中編輯授權回調頁,這里與后面代碼里的回調地址要一致
3、 微博登錄標識的地址如下:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
參數
|
必選 |
類型及范圍 |
說明 |
---|---|---|---|
client_id |
true |
string |
申請應用時分配的AppKey。 |
redirect_uri |
true |
string |
授權回調地址,站外應用需與設置的回調地址一致,站內應用需填寫canvas page的地址。 |
response_type |
false |
string |
code |
返回值
返回值字段 |
字段類型 |
字段說明 |
---|---|---|
code |
string |
用於第二步調用oauth2/access_token接口,獲取授權后的access token。 |
state |
string |
如果傳遞參數,會回傳該參數。 |
4、如果用戶同意授權,頁面跳轉至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE
示例:
http://www.xxxxx.com/callback.php?code=1f5848d0562f39fbbaf8d35cbc853ea3
5、get獲取code值,用CURL方式請求Access Token
請求地址:
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
請求方式:
POST
請求參數:
|
必選 |
類型及范圍 |
說明 |
---|---|---|---|
client_id |
true |
string |
申請應用時分配的AppKey。 |
client_secret |
true |
string |
申請應用時分配的AppSecret。 |
grant_type |
true |
string |
請求的類型,填寫authorization_code |
code |
ture |
string |
調用authorize獲得的code值。 |
rediect_url |
ture |
string |
回調地址,需需與注冊應用里的回調地址一致。 |
返回值
{ "access_token": "SlAV32hkKG", "remind_in": 3600, "expires_in": 3600, "uid":1887188234 }
6、使用獲得的Access Token和uid調用API
獲取用戶信息API:
https://api.weibo.com/2/users/show.json
請求參數:
|
必選 |
類型及范圍 |
說明 |
---|---|---|---|
access_token |
true |
string |
采用OAuth授權方式為必填參數,OAuth授權后獲得。 |
uid |
false |
int64 |
需要查詢的用戶ID。 |
screen_name |
false |
string |
需要查詢的用戶昵稱。 |
參數uid與screen_name二者必選其一,且只能選其一;
返回示例:
{ "id": 1404376560, "screen_name": "zaku", "name": "zaku", "province": "11", "city": "5", "location": "北京 朝陽區", "description": "人生五十年,乃如夢如幻;有生斯有死,壯士復何憾。", "url": "http://blog.sina.com.cn/zaku", "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1", "domain": "zaku", "gender": "m", "followers_count": 1204, "friends_count": 447, "statuses_count": 2908, "favourites_count": 0, "created_at": "Fri Aug 28 00:00:00 +0800 2009", "following": false, "allow_all_act_msg": false, "geo_enabled": true, "verified": false, "status": { "created_at": "Tue May 24 18:04:53 +0800 2011", "id": 11142488790, "text": "我的相機到了。", "source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>", "favorited": false, "truncated": false, "in_reply_to_status_id": "", "in_reply_to_user_id": "", "in_reply_to_screen_name": "", "geo": null, "mid": "5610221544300749636", "annotations": [], "reposts_count": 5, "comments_count": 8 }, "allow_all_comment": true, "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1", "verified_reason": "", "follow_me": false, "online_status": 0, "bi_followers_count": 215 }
返回值:
返回值字段 |
字段類型 |
字段說明 |
---|---|---|
id |
int64 |
用戶UID |
idstr |
string |
字符串型的用戶UID |
screen_name |
string |
用戶昵稱 |
name |
string |
友好顯示名稱 |
province |
int |
用戶所在省級ID |
city |
int |
用戶所在城市ID |
location |
string |
用戶所在地 |
description |
string |
用戶個人描述 |
url |
string |
用戶博客地址 |
profile_image_url |
string |
用戶頭像地址(中圖),50×50像素 |
profile_url |
string |
用戶的微博統一URL地址 |
domain |
string |
用戶的個性化域名 |
weihao |
string |
用戶的微號 |
gender |
string |
性別,m:男、f:女、n:未知 |
followers_count |
int |
粉絲數 |
friends_count |
int |
關注數 |
statuses_count |
int |
微博數 |
favourites_count |
int |
收藏數 |
created_at |
string |
用戶創建(注冊)時間 |
following |
boolean |
暫未支持 |
allow_all_act_msg |
boolean |
是否允許所有人給我發私信,true:是,false:否 |
geo_enabled |
boolean |
是否允許標識用戶的地理位置,true:是,false:否 |
verified |
boolean |
是否是微博認證用戶,即加V用戶,true:是,false:否 |
verified_type |
int |
暫未支持 |
remark |
string |
用戶備注信息,只有在查詢用戶關系時才返回此字段 |
status |
object |
用戶的最近一條微博信息字段 詳細 |
allow_all_comment |
boolean |
是否允許所有人對我的微博進行評論,true:是,false:否 |
avatar_large |
string |
用戶頭像地址(大圖),180×180像素 |
avatar_hd |
string |
用戶頭像地址(高清),高清頭像原圖 |
verified_reason |
string |
認證原因 |
follow_me |
boolean |
該用戶是否關注當前登錄用戶,true:是,false:否 |
online_status |
int |
用戶的在線狀態,0:不在線、1:在線 |
bi_followers_count |
int |
用戶的互粉數 |
lang |
string |
用戶當前的語言版本,zh-cn:簡體中文,zh-tw:繁體中文,en:英語 |
如圖,此時就可以獲取想要的用戶信息(用戶昵稱、頭像等),可讓用戶直接登錄訪問網站了
其實像微信登陸、QQ登陸的原理都一樣,都是:
1、獲取用戶授權,取得code
2、將code發送到授權服務器獲取Access Token
3、通過Access Token調取API接口獲取用戶信息