iOS 友盟實現新浪微博和第三方登陸,ios新浪
一、第三方登陸
1.參照友盟社會化分享文檔進行配置
2.在登入按鈕處添加對應代碼可以獲得UMSocialAccountEntity *snsAccount包括
@property (nonatomic, copy) NSString *platformName;
@property (nonatomic, copy) NSString *usid;
@property (nonatomic, copy) NSString *accessToken;
@property (nonatomic, copy) NSString *openId;
@property (nonatomic, copy) NSString *refreshToken;等資訊
二、第三方登陸資訊管理方案
獲得UMSocialAccountEntity *snsAccount後,調用伺服器介面,由伺服器進行資料的抓取和賦值,返回登陸成功或失敗資訊。
上傳獲得使用者資料的介面所需要的參數
sns-type //平台類型
usid //所在平台usid
access_token
openid
伺服器用的介面:
擷取資料介面: https://api.weixin.qq.com/sns/userinfo
請求方式: get
請求參數: 參數 必選
access_token 必填
openid 必填
返回參數:
{
"openid":"OPENID",
"nickname":"NICKNAME",
"sex":1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
"privilege":[
"PRIVILEGE1",
"PRIVILEGE2"
],
"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
文檔地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN
新浪微博擷取資料介面:https://api.weibo.com/2/users/show.json
請求方式:get
請求參數 參數 必選
access_token 必填
usid 必填
返回參數:{
"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
}
文檔地址:http://open.weibo.com/wiki/2/users/show
三、第三方登陸access_token有效期間管理
為了更好地獲得使用者資訊,可能需要對access_token的有效期間進行管理。下面以新浪微博和為例說明。
1.access_token有效期間
新浪微博普通授權層級的access_token的授權有效期間是7天,測試層級為1天。
access_token有效期間(目前為2個小時)較短。
2.使用refresh_token延長授權時間,避免頻繁的調用授權介面讓使用者授權。
當access_token逾時後,可以使用refresh_token進行重新整理,access_token重新整理結果有兩種:
- 若access_token已逾時,那麼進行refresh_token會擷取一個新的access_token,新的逾時時間;
- 若access_token未逾時,那麼進行refresh_token不會改變access_token,但逾時時間會重新整理,相當於續期access_token。refresh_token擁有較長的有效期間(30天),當refresh_token失效的後,需要使用者重新授權。
3.refresh_token的介面
新浪微博
介面URL:https://api.weibo.com/oauth2/access_token
請求方式:POST
請求參數:
參數 必選 類型及範圍 說明
client_id true string 申請應用時分配的AppKey。
client_secret true string 申請應用時分配的AppSecret。
grant_type true string 請求的類型即-refresh_token
redirect_uri true string 回調地址,需需與註冊應用裡的回調
地址一致。
refresh_token true string refresh_token
返回資料:
{
"access_token": "ACCESS_TOKEN",
"expires_in": 1234,
"remind_in":"798114",
"uid":"12341234"
}
要求方法:
介面URL:https://api.weixin.qq.com/sns/oauth2/refresh_token
請求類型:GET
請求參數:
參數 是否必須 說明
appid 是 應用唯一標識
grant_type 是 填refresh_token
refresh_token 是 填寫通過access_token擷取到的
refresh_token參數
返回資料:
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
4.邏輯順序
- 當進入應用時,進行使用者授權到期檢查,如果使用者是第三方登陸且refresh_token到期,怎進行第三方登陸授權提醒
- 當涉及access_token使用時,有伺服器進行如下操作 a.如果沒到期,就直接請求。
b.如果到期,判斷refresh_token是否到期,沒有到期就重新整理access_token,然後請求。
c.refresh_token沒有到期,提醒用戶端進行授權處理。
第一步也可以不要。
5.需要上傳參數
只要多上傳 refresh_token 就行了。看下微博和的授權延長的介面就明白了。
參數 必選 類型及範圍 說明
client_id true string 申請應用時分配的AppKey。
client_secret true string 申請應用時分配的AppSecret。
grant_type true string 請求的類型即 refresh_token
redirect_uri true string 回調地址,需需與註冊應用裡的回調
地址一致。
refresh_token true string refresh_token
參數 是否必須 說明
appid 是 應用唯一標識
grant_type 是 填refresh_token
refresh_token 是 填寫通過access_token擷取到的
refresh_token參數
新浪微博和的請求參數除了refresh_token都是固定不變的。
四、總結
1.使用者端只需要在授權時,調用一個介面,進行第三方登陸操作,在只有和新浪微博的情況下,只需要設計一個包含如下參數的介面即可:
sns-type //平台類型
usid //所在平台usid
access_token
openid //可以和usid複用
refresh_token
由伺服器進行access_token時間的管理即可。
2.在啟動應用時,進行判斷,如果到期進行授權操作(可選)。