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