iOS中OAuth授權獲取Access_Tolen
在所有快速登錄中差不多采用同樣的授權方式獲取access_Token
(Oauth2.0
授權獲取Access_Token
),今天以新浪微博獲取Access_Token
:
1 成為為微博開發者
首先登錄微博開放平臺
http://open.weibo.com
先注冊成為開發者,驗證郵箱之后.
2 創建應用
點擊
微鏈接-移動應用-立即接入-繼續創建
.創建應用名稱(最好和你工程名稱相同).
創建完成之后
基本應用信息
系統自動為該應用生成的APPKey
和APPSecret
.并在應用信息的
高級信息
,設置授權回調頁
地址Redirect_URI
.(隨便填寫,可以寫百度"https://www.baidu.com")由于這里是手機客戶端,而不是web應用,因此創建應用的時候,
Redirect_URI
可以隨便寫,但必須全局都使用同一個地址Redirect_URI
.取消授權回調頁
也是隨便填寫.3 顯示登錄頁面,請求用戶授權:
點擊文檔-微博登陸-授權機制-接口;
-
2.必傳參數
- client_id 申請應用時分配的AppKey
- redirect_uri 授權回調地址
3.完整的請求路徑:https://api.weibo.com/oauth2/authorize?client_id=4067793982&redirect_uri=http://www.baidu.com
4.用戶輸入賬號密碼,授權成功后
1.會自動定位到回調地址
2.新浪會在回調地址的后面拼接一個code參數 (將來要利用code參數向新浪服務器換取一個access_token)
這個code可以在webView的代理方法中拿到.5.利用code參數向新浪服務器換取一個access_token
具體實現代碼如下:
// "access_token" = "2.00vWf4GE3CDS8E082f1f060fSdU2jD"
// 一個accessToken對應著 1個用戶 + 1個應用
#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>
/**
* 每當webView想發送請求之前都會調用(能在這個方法中攔截webView的所有請求)
*
* @param request webView想發送的請求
*
* @return YES : 允許加載這個請求, NO : 禁止加載這個請求
*/
- (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) { // 已經找到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
// 請求方式 : POST
/*請求參數
client_id 申請應用時分配的AppKey。
client_secret 申請應用時分配的AppSecret。
grant_type 請求的類型,填寫authorization_code
code 調用authorize獲得的code值。
redirect_uri 回調地址,需需與注冊應用里的回調地址一致。
*/
/*返回結果
{
"access_token": "ACCESS_TOKEN",
"expires_in": 1234,
"remind_in":"798114",
"uid":"12341234"
}
*/
// 1.創建一個請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.拼接請求參數
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.發送一個POST請求
[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"];
// 將服務器返回的字典(JSON)數據存儲起來
[responseObject writeToFile:path atomically:YES];
// 切換到主控制器(獲取到之后要跳轉頁面)
[UIApplication sharedApplication].keyWindow.rootViewController = [[HWTabBarController alloc] init];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HWLog(@"請求失敗 - %@", error);
}];
}
@end