OAuth authorization details, take Sina Weibo as an example

Source: Internet
Author: User
Tags oauth

/*
OAuth Authorization Process:
1. Login account
Http://open.weibo.com
Sign up for developer information and become a SINA developer

2, click "Mobile App", create an app (tick app name, application platform on it), page jump, display to "development stage can"

3, back to the main page, click API Interface "Home" OAuth2.0 Authorization certification
Follow the instructions click to enter (1) oauth2/authorize get code (use GET request)
Click to enter (2) Oauth2/access_token get token (use POST request)

4. Save the acquired token for future use of Weibo data.

5, return to the API home page, into the "micro-bo" Statuses/home_timeline "as required to use token to obtain micro-blog data

*/

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Create Web
Nsurlrequest * Requset = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "Https://api.weibo.com/oauth2/authorize? Client_id=3025058065&redirect_uri=www.baidu.com "];
UIWebView * web = [[UIWebView alloc] initWithFrame:self.view.bounds];
[Self.view Addsubview:web];
[Web Loadrequest:requset];
Web.delegate = self;
}

Each time a token is acquired, a new code is needed. But token gets it once and saves it.

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

Intercept the URL containing code and remove the code
NSString * Path = Request. url.absolutestring;
BOOL iscode = [path containsstring:@ "code="];
NSString * SUB;
if (Iscode)
{
Nsrange range = [path rangeofstring:@ "code="];
Sub = [path substringfromindex:range.location+5];
Self.code = Sub;

Obtain tokens according to code (the obtained token is saved for use when the next run)
[Self getToken];

}

return YES;
}


-(void) GetToken
{
Afhttprequestoperationmanager * Manager = [Afhttprequestoperationmanager manager];
Nsdictionary * dic = @{@ "client_id": @ "3025058065", @ "Client_secret": @ "867adcfb83eeebe64394629fafefea41", @ "Grant_ Type ": @" Authorization_code "@" code ": Self.code, @" Redirect_uri ": @" www.baidu.com "};

Manager.responseserializer = [Afhttpresponseserializer serializer];//because the data returned by Sina is not marked as JSON type, so this is accepted using data type, Otherwise, the request fails.

[Manager post:@ "Https://api.weibo.com/oauth2/access_token" Parameters:dic success:^ (Afhttprequestoperation * Operation, NSData * responseobject) {

JSON into objects
Nsdictionary * dic = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil];
Self.dic = dic;

Access to Weibo data
[Self getStatus];
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "%@", error);
}];
}


-(void) getStatus
{
NSString * token = self.dic[@ "Access_token"];
NSString * str = [NSString stringwithformat:@ "https://api.weibo.com/2/statuses/home_timeline.json?access_token=%@", Token];

Request Micro-Blog data
Afhttprequestoperationmanager * Manager = [Afhttprequestoperationmanager manager];
[Manager get:str Parameters:nil success:^ (afhttprequestoperation *operation, nsdictionary * responseobject) {

NSLog (@ "-------%@", responseobject);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {

NSLog (@ "%@", error);
}];

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OAuth authorization details, take Sina Weibo as an example

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.