IOS youmeng implements third-party login on Sina Weibo and WeChat, ios Sina

Source: Internet
Author: User
Tags openid

IOS youmeng implements Sina Weibo and third-party login, ios Sina

1. Third-party Login

1. Configure with reference to umeng social sharing document

2. Add the corresponding code at the login button to get UMSocialAccountEntity * snsAccount includes

@ 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; and other information

Ii. Third-party login information management solution

After obtaining UMSocialAccountEntity * snsAccount, the server interface is called to capture and assign values to the data by the server, and the login success or failure information is returned.

Parameters required for the interface for uploading and Obtaining user data

Sns-type // platform type

Usid // platform usid

Access_token

Openid

Server interface:

Getting materials interface: https://api.weixin.qq.com/sns/userinfo

Request Method: get

Request Parameter: Required

Access_token is required

Openid required

Response parameters:

{

"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"

 

}

Document address: https://open.weixin.qq.com/cgi-bin/showdocument? Action = dir_list & t = resource/res_list & verify = 1 & id = open1419317853 & token = & lang = zh_CN

Sina Weibo get information interface: https://api.weibo.com/2/users/show.json

Request Method: get

Required for Request Parameter

Access_token is required

Usid required

Response parameters :{

"Id": 1404376560,

"Screen_name": "zaku ",

"Name": "zaku ",

"Province": "11 ",

"City": "5 ",

"Location": "Chaoyang District, Beijing ",

"Description": "fifty years of life is like a dream. If there is life, there is death. ",

"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": "My camera is here. ",

"Source": "<a href =" http://weibo.com "rel =" nofollow "> Sina Weibo </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

}

Document address: http://open.weibo.com/wiki/2/users/show

3. Third-party login access_token Validity Period Management

To better obtain user information, you may need to manage the validity period of access_token. The following uses Sina Weibo as an example.

1. access_token Validity Period

The validity period of the access_token of Sina Weibo normal authorization level is 7 days, and the test level is 1 day.

The validity period of access_token (currently 2 hours) is short.

2. Use refresh_token to extend the authorization time to avoid frequent calls to the authorization interface for user authorization.

When the access_token times out, you can use refresh_token to refresh. There are two types of access_token refresh results:

  • If access_token has timed out, refresh_token will get a new access_token and a new timeout time;
  • If access_token does not time out, refresh_token will not change access_token, but will refresh the timeout, which is equivalent to renewing access_token. Refresh_token has a long validity period (30 days). If refresh_token fails, you need to re-authorize it.

3. refresh_token Interface

Sina Weibo

Interface URL: https://api.weibo.com/oauth2/access_token

Request Method: POST

Request Parameters:

Description of required parameter types and ranges

Client_id true string the AppKey allocated when the application is applied.

Client_secret true string the AppSecret allocated when the application is applied.

Grant_type true string request type-refresh_token

Redirect_uri true string callback address, which must be the same as the callback in the registered application

The addresses are the same.

Refresh_token true string refresh_token

Returned data:

{

"Access_token": "ACCESS_TOKEN ",

"Expires_in": 1234,

"Remind_in": "798114 ",

"Uid": "12341234"

}

Request Method:

Interface URL: https://api.weixin.qq.com/sns/oauth2/refresh_token

Request type: GET

Request Parameters:

Required parameter description

Appid is the unique ID of an application.

Grant_type indicates refresh_token.

Refresh_token is obtained through access_token.

Refresh_token Parameter

Returned data:

{

"Access_token": "ACCESS_TOKEN ",

"Expires_in": 7200,

"Refresh_token": "REFRESH_TOKEN ",

"Openid": "OPENID ",

"Scope": "SCOPE"

}

4. Logical Order

  • When you enter an application, perform a user authorization expiration check. If the user logs on to a third party and the refresh_token expires, how does one send a third-party logon authorization reminder?
  • When access_token is involved, some servers perform the following operations. a. If it does not expire, directly request it.

B. If the token expires, check whether the refresh_token expires. If the token does not expire, refresh the access_token and request it.

C. If the refresh_token does not expire, the client is reminded to perform authorization.

You can skip this step.

5. You need to upload parameters.

You only need to upload refresh_token. Let's take a look at Weibo and the authorization extension interface.

Description of required parameter types and ranges

Client_id true string the AppKey allocated when the application is applied.

Client_secret true string the AppSecret allocated when the application is applied.

Grant_type true string request type is refresh_token

Redirect_uri true string callback address, which must be the same as the callback in the registered application

The addresses are the same.

Refresh_token true string refresh_token

Required parameter description

Appid is the unique ID of an application.

Grant_type indicates refresh_token.

Refresh_token is obtained through access_token.

Refresh_token Parameter

In addition to refresh_token, The Request Parameters of Sina Weibo and Sina Weibo are fixed.

Iv. Summary

1. the user only needs to call an interface during authorization to log on to a third party. In the case of Sina Weibo and only Sina Weibo, you only need to design an interface containing the following parameters:

Sns-type // platform type

Usid // platform usid

Access_token

Openid // can be reused with usid

Refresh_token

The server can manage the access_token time.

2. When starting an application, make a judgment. If the application expires, perform authorization (optional ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.