星空网 > 软件开发 > 操作系统

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.在启动应用时,进行判断,如果过期进行授权操作(可选)。




原标题:iOS 友盟实现新浪微博和微信第三方登陆

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流