OAuth Authorization Callback Accesstoken

Source: Internet
Author: User
Tags oauth

For details, see Sina official documentsHttp://open.weibo.com/wiki/Oauth2/access_token Concrete Implementation

The first step is to open the callback page

Macro Definition client_id

#define KCLIENTID @ "XXX"

Macro Definition Callback Address

#define Kredirect_uri @"xxx"

Macro Definition Client_secret

#define Kclient_secret @ "XXX"

1. Add WebView

UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.bounds];

Webview.delegate = self;

[Self.view Addsubview:webview];

2. Loading Authorization page

NSString *oauthpath = [NSString stringwithformat:@ "https://api.weibo.com/oauth2/authorize?client_id=%@& redirect_uri=%@ ", Kclientid, Kredirect_uri];

Nsurl *url = [Nsurl Urlwithstring:oauthpath];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

[WebView Loadrequest:request];

/**

* This method is called before WebView sends a request, asking if the agent can load the page (request)

*

* @return YES: Can load page, no: Can not load page

*/

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {

Second step to get request parameters

1. The absolute path of the URL returned by the request HTTP://WWW.BAIDU.COM/?CODE=A0872F25A06ACF714A4067266A2EEAF4

NSString *requesturlpath = Request. url.absolutestring;

2. Access to the scope of the code=

Nsrange range = [Requesturlpath rangeofstring:@ "? code="];

3. If found in Requesturlpath? code= to intercept

if (range.length) {//if (range.location! = Nsnotfound) Same as Left

Add the code= length to the code= position? string after code=

int loc = range.location + range.length;

NSString *code = [Requesturlpath substringfromindex:loc];

The third step is to get the Accesstoken by the post transfer parameter body

Afnetworking Frame

  Create a Request Management object

Afhttprequestoperationmanager *manager = [Afhttprequestoperationmanager manager];

//Post mode access needs to pass the parameter body, stitching the parameters that need to be passed

nsmutabledictionary *askparams = [nsmutabledictionary dictionary];

askparams[@ "client_id"] = Kclientid;

askparams[@ "Client_secret"] = Kclient_secret;

askparams[@ "Grant_type"] = @ "Authorization_code";

askparams[@ "Code" = code;

askparams[@ "Redirect_uri"] = Kredirect_uri;

//Send POST request

[manager post:@ "Https://api.weibo.com/oauth2/access_token" Parameters:askparams success:^ ( Afhttprequestoperation *operation, id responseobject) {

Xylog (@ "%@--responseobject", responseobject);

} failure:^ (Afhttprequestoperation *operation, Nserror *error) {

Xylog (@ "%@--error", error);

}];

}

return YES;

}

The parameters for the post request to be stitched include

/**

client_idString The Appkey that is assigned when the application is applied.

Client_secretString The Appsecret that is assigned when the application is applied.

Grant_typetype of string request, fill in Authorization_code

Code string calls the code value obtained by authorize.

The Redirect_uristring callback address needs to match the callback address in the registration application.

*/(see Http://open.weibo.com/wiki/Oauth2/access_token)

The return value example is as follows

{

"Access_token": "Access_token",

"Expires_in": 1234,

"Remind_in": "798114",

"UID": "12341234"

}

OAuth Authorization Callback Accesstoken

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.