Note: Weibo open interface calls, such as Micro bo, attention, etc., are required to obtain user authentication. At present, the user identity authentication of Weibo open platform is mainly based on OAuth2.0. To make it easier for developers to develop and test their applications.
The OAuth2.0 is simpler and more secure than 1.0, and is the most important way to authenticate and authorize users in the future.
Step one: Create an app
Below I take the company test account as an example, to create an application step can refer to Sina's official API address: http://open.weibo.com application to create a good stay in the development phase can be used, this example of application information such as
Step two: Get the token number
Via WebView load link where client_id for app Key, Redirect_uri value for company jump link Here I take the company link as an example
UIWebView * Web=[[uiwebview alloc] init]; web.frame=self.view.bounds; Nsstring*[email protected] "https://api.weibo.com/oauth2/authorize?client_id=3272733387&redirect_uri=http:// Www.21-sun.com "; Nsurl * Url=[nsurl URLWITHSTRING:STR]; Nsurlrequest *request=[nsurlrequest Requestwithurl:url]; [Web Loadrequest:request]; [Self.view addsubview:web];web.delegate=self;
The effect interface is as follows, login complete authorization:
In the back of the link will be spelled with the parameter code, this code we need to spare, we can use the WebView proxy to intercept the return link
#pragma mark-Allow proxy load request-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) request Navigationtype: (uiwebviewnavigationtype) navigationtype{nsstring * str=request. Url.absolutestring;if ([str containsstring:@ "http://www.21-sun.com/?code="]) { Nsinteger index=[str rangeofstring:@ "="].location; NSString * Code=[str substringfromindex:index+1]; return NO; }return YES;}
Request Access_token, use the link below to request
client_id true string to be assigned when applying the Appkey.
Client_secret true string to be assigned when applying the Appsecret.
Grant_type true string request type, fill in Authorization_code
Code true string above the code value obtained.
Redirect_uri true string callback address, which needs to be consistent with the callback address in the registration application.
The code is as follows
-(void) _gettoken: (NSString *) code{ nsdictionary *[email protected]{@ "client_id": @ "3272733387" @ "Client_secret ": @" 10003f9922c9d0e0fefb03500c8d4dbc "@" Grant_type ": @" Authorization_code "@" code ":d ata,@" Redirect_uri ": @" http ://www.21-sun.com "}; Afhttprequestoperationmanager * Manager=[afhttprequestoperationmanager manager]; Manager.responseserializer.acceptablecontenttypes=[nsset setwithobject:@ "Text/plain"]; [Manager post:@ "Https://api.weibo.com/oauth2/access_token" Parameters:dic success:^ (afhttprequestoperation *operation, Nsdictionary * responseobject) { NSString * token=responseobject[@ "Access_token"]; } failure:^ ( Afhttprequestoperation *operation, Nserror *error) {NSLog (@ "request Failed");}];}
At this point we can do a lot of things with the Access_token code we get.
Jerry Education
Source:http://blog.csdn.net/jerehedu
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
iOS development Sina Weibo OAuth2