iOS中OAuth授權(quán)獲取Access_Tolen
在所有快速登錄中差不多采用同樣的授權(quán)方式獲取access_Token(Oauth2.0授權(quán)獲取Access_Token),今天以新浪微博獲取Access_Token:
1 成為為微博開(kāi)發(fā)者
首先登錄微博開(kāi)放平臺(tái)
http://open.weibo.com先注冊(cè)成為開(kāi)發(fā)者,驗(yàn)證郵箱之后.
2 創(chuàng)建應(yīng)用
點(diǎn)擊
微鏈接-移動(dòng)應(yīng)用-立即接入-繼續(xù)創(chuàng)建.創(chuàng)建應(yīng)用名稱(chēng)(最好和你工程名稱(chēng)相同).
創(chuàng)建完成之后
基本應(yīng)用信息系統(tǒng)自動(dòng)為該應(yīng)用生成的APPKey和APPSecret.并在應(yīng)用信息的
高級(jí)信息,設(shè)置授權(quán)回調(diào)頁(yè)地址Redirect_URI.(隨便填寫(xiě),可以寫(xiě)百度"https://www.baidu.com")由于這里是手機(jī)客戶(hù)端,而不是web應(yīng)用,因此創(chuàng)建應(yīng)用的時(shí)候,
Redirect_URI可以隨便寫(xiě),但必須全局都使用同一個(gè)地址Redirect_URI.取消授權(quán)回調(diào)頁(yè)也是隨便填寫(xiě).3 顯示登錄頁(yè)面,請(qǐng)求用戶(hù)授權(quán):
點(diǎn)擊文檔-微博登陸-授權(quán)機(jī)制-接口;1.頁(yè)面地址:https://api.weibo.com/oauth2/authorize
-
2.必傳參數(shù)
- client_id 申請(qǐng)應(yīng)用時(shí)分配的AppKey
- redirect_uri 授權(quán)回調(diào)地址
3.完整的請(qǐng)求路徑:https://api.weibo.com/oauth2/authorize?client_id=4067793982&redirect_uri=http://www.baidu.com
4.用戶(hù)輸入賬號(hào)密碼,授權(quán)成功后
1.會(huì)自動(dòng)定位到回調(diào)地址
2.新浪會(huì)在回調(diào)地址的后面拼接一個(gè)code參數(shù) (將來(lái)要利用code參數(shù)向新浪服務(wù)器換取一個(gè)access_token)
這個(gè)code可以在webView的代理方法中拿到.5.利用code參數(shù)向新浪服務(wù)器換取一個(gè)access_token
具體實(shí)現(xiàn)代碼如下:
// "access_token" = "2.00vWf4GE3CDS8E082f1f060fSdU2jD"
// 一個(gè)accessToken對(duì)應(yīng)著 1個(gè)用戶(hù) + 1個(gè)應(yīng)用
#import "HWOAuthViewController.h"
#import "AFNetworking.h"
#import "HWTabBarController.h"
@interface HWOAuthViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation HWOAuthViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=1281603749&redirect_uri=https://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#pragma mark - <UIWebViewDelegate>
/**
* 每當(dāng)webView想發(fā)送請(qǐng)求之前都會(huì)調(diào)用(能在這個(gè)方法中攔截webView的所有請(qǐng)求)
*
* @param request webView想發(fā)送的請(qǐng)求
*
* @return YES : 允許加載這個(gè)請(qǐng)求, NO : 禁止加載這個(gè)請(qǐng)求
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// 1.URL字符串
NSString *url = request.URL.absoluteString;
// 2.查找URL中是否有"code="
NSRange range = [url rangeOfString:@"code="];
if (range.length) { // 已經(jīng)找到code=
// 3.截取code
NSString *code = [url substringFromIndex:range.location + range.length];
// 4.利用code換取access_token
[self accessTokenWithCode:code];
return NO;
}
return YES;
}
/**
* 利用code換取access_token
*/
- (void)accessTokenWithCode:(NSString *)code
{
// URL : https://api.weibo.com/oauth2/access_token
// 請(qǐng)求方式 : POST
/*請(qǐng)求參數(shù)
client_id 申請(qǐng)應(yīng)用時(shí)分配的AppKey。
client_secret 申請(qǐng)應(yīng)用時(shí)分配的AppSecret。
grant_type 請(qǐng)求的類(lèi)型,填寫(xiě)authorization_code
code 調(diào)用authorize獲得的code值。
redirect_uri 回調(diào)地址,需需與注冊(cè)應(yīng)用里的回調(diào)地址一致。
*/
/*返回結(jié)果
{
"access_token": "ACCESS_TOKEN",
"expires_in": 1234,
"remind_in":"798114",
"uid":"12341234"
}
*/
// 1.創(chuàng)建一個(gè)請(qǐng)求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.拼接請(qǐng)求參數(shù)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"client_id"] = @"1281603749";
params[@"client_secret"] = @"443af14f07509433acef44d9fc158e53";
params[@"grant_type"] = @"authorization_code";
params[@"code"] = code;
params[@"redirect_uri"] = @"https://www.baidu.com";
// 3.發(fā)送一個(gè)POST請(qǐng)求
[mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params
success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// 沙盒路徑
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"account.plist"];
// 將服務(wù)器返回的字典(JSON)數(shù)據(jù)存儲(chǔ)起來(lái)
[responseObject writeToFile:path atomically:YES];
// 切換到主控制器(獲取到之后要跳轉(zhuǎn)頁(yè)面)
[UIApplication sharedApplication].keyWindow.rootViewController = [[HWTabBarController alloc] init];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HWLog(@"請(qǐng)求失敗 - %@", error);
}];
}
@end