Eventually:
Oauthviewcontroller.m
oauthviewcontroller.m//20_ Handsome no Weibo////Created by Beyond on 14-8-5.//Copyright (c) 2014 Com.beyond. All rights reserved.//Authorization controller, run only once, obtain the current user's Access_token and UID, archive, switch the window's main controller #import "OauthViewController.h" @ Interface Oauthviewcontroller () <uiwebviewdelegate>{//member variables remember that UIWebView *_webview is used in different methods;} @end @implementation oauthviewcontroller-(void) loadview{//Direct webView to be the controller view, avoid adding _webview = [[UIWebView alloc] INIT]; Self.view = _webview;} -(void) viewdidload{[Super Viewdidload]; Set the agent as the current controller so that the listener WebView starts loading and ends loading _webview.delegate = self; Application for certification address nsstring *oauthurl = [NSString stringwithformat:@ "https://api.weibo.com/oauth2/authorize?client_id=%@ &response_type=code&redirect_uri=%@ ", Kappkey,kredirecturl]; Call the classification method and load the URL of the application for authentication [_webview Loadurlstring:oauthurl];} #pragma mark-Proxy method//Start loading-(void) Webviewdidstartload: (UIWebView *) webview{log (@ "Really start loading--%@", WebView.request.URL); Start loading on, show progress bar mbprogrEsshud *hud = [Mbprogresshud showhudaddedto:webview animated:yes]; Hud.labeltext = @ "page loaded ...";} Whether to start loading a page-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype :(Uiwebviewnavigationtype) navigationtype{log (@ "Can load--%@", WebView.request.URL); return YES;} Page load complete-(void) Webviewdidfinishload: (UIWebView *) webview{log (@ "Load complete--%@", WebView.request.URL); Once loaded, hide the progress bar [Mbprogresshud Hideallhudsforview:webview Animated:yes]; After the user has consented to the authorization, the returned URL contains an authorized Request_code, such as: http://www.abc.com/?code=888888888888//After returning the user-authorized Request_code page, Need to intercept code, and then continue stitching the URL, initiating the 3rd request (this time must be post), and finally return the required access_token nsstring *redirecturlcontainscode = _webview.request.u rl.absolutestring; Classification method, starting from the last face of the left tag string, nsstring *code = [Redirecturlcontainscode substrfromleftflagstr:@ "? code="]; If it is not a URL to return code, do nothing if (code = = nil) return; Now ready to launch the last request, splicing the 3rd request of the required URL, the request returned to the east, will be the most important user Accesstoken, also contains the userThe uid nsstring *accesstokenrequesturlstr = [NSString stringwithformat:@ "Https://api.weibo.com/oauth2/access_token? client_id=%@&client_secret=%@&grant_type=authorization_code&redirect_uri=%@&code=%@ ", KAppKey, Kappsecret,kredirecturl,code]; 1, create URL nsurl *accesstokenrequesturl = [Nsurl urlwithstring:accesstokenrequesturlstr]; 2, create POST request nsmutableurlrequest *mutrequest = [[Nsmutableurlrequest alloc]initwithurl:accesstokenrequesturl Cachepolicy:nsurlrequestuseprotocolcachepolicy Timeoutinterval:10]; The set request mode is POST, the default is get [Mutrequest sethttpmethod:@ "POST"]; 3, connect the server, and receive the returned data nsdata *receiveddata = [nsurlconnection sendsynchronousrequest:mutrequest returningresponse:nil ER Ror:nil]; Turn the data returned by the server into a string (essentially JSON data) nsstring *RESPONSESTR = [[NSString alloc]initwithdata:receiveddata encoding: Nsutf8stringencoding]; Log (@ "Response json is:%@", RESPONSESTR); 4, from RESPONSESTR (essentially JSON data) get to Access_token//Will (JSON data) into a dictionary first Nsdictionary *dictionary = [nsjsonserialization jsonobjectwithdata:receiveddata options: Nsjsonreadingmutablecontainers Error:nil]; By key, take to access_token nsstring *access_token = [Dictionary objectforkey:@ "Access_token"]; Log (@ "access token is:%@", access_token); Through the key, take the user uid nsstring *uid = [Dictionary objectforkey:@ "UID"]; Log (@ "UID is:%@", UID); Authorization succeeded, switch root controller to host controller uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle:@ ' authorization succeeded ' Delegate:nil CancelButton title:@ "Cancel" destructivebuttontitle:@ "OK" otherbuttontitles:nil]; [Actionsheet ShowInView:self.view.window];} @end
Additional notes:
No. 0 Step,
Once you've registered as a developer and verified your mailbox, you can create a mobile app,
Note the Appkey and Appsecretthat the system automatically generates for the app,
And in the advanced information of the application information, set the address of the callback page for authorization completion Redirect_uri
Since this is a mobile client, not a web app,
So when creating an app,Redirect_uri can write casually,
But you must use the same address globally Redirect_uri
1th Step,
To apply for unauthorised request_code,
The essence is the login page of the microblog, which is the first URL loaded by _webview.
The address format is as follows:
https://api.weibo.com/oauth2/authorize?client_id=APPKEY&response_type=code&redirect_uri=https:// Api.weibo.com/oauth2/default.html
Appkey is the only app ID that the system automatically generates when you create an app
Redirect_uri, required and the same as when creating your own fill in the application
2nd step,
After the user has entered the account number and password, click Sign In,
The page will automatically go to the authorization page,
If the user clicks the authorization button, at this point, the page is redirected to http://redirectURL/?code=888888888888,
The job to do is to intercept the code value in the redirected URL (which is different every time ),
This code is actually an authorized Request_code,
But it's just a middleman, and it can't be used to get users ' information.
The address format is as follows:
Https://api.weibo.com/oauth2/default.html?code=fa4efb6310411f948423e69adeabec08
3rd Step,
With the code captured in the 2nd step, assemble the URL again,
Initiate the last request (must be a POST request),
At this point, the data returned by the server is a required JSON data.
It contains three key-value pairs
{
"Access_token": "This is the real Access_token",
"Remind_in": "157679999",
"Expires_in": 157679999,
"UID": "UID of the user authorized"
}