IOS development: Sina Weibo oau22.

Source: Internet
Author: User

IOS development: Sina Weibo oau22.

Note: You must obtain user identity authentication when calling open interfaces on Weibo, such as Weibo and followers. Currently, oauth2.0. To facilitate developers to develop and test their own applications.

Compared with OAuth2.0, OAuth2.0 is simpler and safer than 1.0. It is also the most important way to authenticate and authorize users in the future.

Step 1: Create an application

Below I take this company test account as an example, the creation of application steps can refer to Sina's official API address: http://open.weibo.com application creation can be used at the development stage, this example of application information such

Step 2: Obtain the token number

Load the link through webView. client_id is the app Key of the application, and redirect_uri is the company jump link. Here I use this company link as an example.

 
UIWebView * web=[[UIWebView alloc] init]; web.frame=self.view.bounds; NSString*str=@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:

The returned link is followed by the parameter code. We need to use this code. We can use the webView proxy to intercept the returned link.

 
# Pragma mark-allow the proxy to load requests-(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, using the following link request

// 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, fill in authorization_code

// Code true string the code value obtained above.

// Redirect_uri true string callback address, which must be consistent with the callback address in the registered application.

  The Code is as follows:

 
-(Void) _ getToken :( NSString *) code {NSDictionary * dic =@{@ client_id: @ 3272733387, @ client_secret: @ cret, @ grant_type: @ authorization_code, @ code: data, @ 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) ;}];}

Now we can use the access_token code we get to do a lot of things.

 

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.