很久沒寫博客,最近在搞一個新浪微博的第三方應用,涉及到瞭Oauth2.0授權獲取Access_Token,特此記錄分享!
步驟一:添加應用
進入新浪微博開放平臺(沒有的話自行註冊),進入“管理中心“,點擊”創建應用”,選擇“微鏈接應用”,再點擊“創建應用”,,選“移動應用”,填寫相應的信息,其中應用地址沒有的話可隨便,勾選平臺後提交。註意保存你的App Key和App Secret以備後用。
步驟二:Oauth2.0授權設置
應用創建完後可以在“管理中心”-“我的應用”中查看信息,在“應用信息”–“高級信息”中可以設置網站的授權回調頁和取消授權回調頁。授權回調頁會在用戶授權成功後會被回調,同時傳回一個“code”參數,開發者可以用code換取Access_Token值。當然如果是移動應用,比如本文是沒有自己授權回調頁的,建議這裡填:https://api.weibo.com/oauth2/default.html 或者 https://www.baidu.com 之類的。如果授權後傳回的形式如下:
https://api.weibo.com/oauth2/default.html?code=a6146547f981199c07348837b0629d5d
我們隻要獲取其中code的值a6146547f981199c07348837b0629d5d即可,註意code的值每次都是不一樣的。
步驟三:引導用戶授權
引導需要授權的用戶到如下頁面:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
YOUR_CLIENT_ID:即應用的AppKey,可以在應用基本信息裡查看到。
YOUR_REGISTERED_REDIRECT_URI:即之前填寫的授權回調頁,註意一定要和你在開發平臺填寫的完全相同,這裡以https://api.weibo.com/oauth2/default.html 為例。
如果用戶授權成功後,會跳轉到回調頁,開發者此時需要得到url參數中的code值,註意code隻能使用一次。
步驟四:換取Access Token
開發者可以訪問如下頁面得到Access Token:
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
YOUR_CLIENT_ID:即應用的AppKey,可以在應用基本信息裡查看到。
YOUR_CLIENT_SECRET:即應用的App Secret,可以在應用基本信息裡查看到。
YOUR_REGISTERED_REDIRECT_URI:即之前填寫的授權回調頁
code:就是步驟三引導用戶授權後回傳的code。
如果都沒有問題,就可以得到Access Token瞭,返回示例:
{
access_token: ACCESS_TOKEN,
expires_in: 1234,
remind_in:798114,
uid:12341234
}
最後做瞭一個Xcode 5.0 storyboard的demo,用到一個UIViewController和一個UIWebView。
看代碼如下:
#import @interface OAuthWebViewController : UIViewController @property (weak, nonatomic) IBOutlet UIWebView *webView; @end
#import OAuthWebViewController.h @implementation OAuthWebViewController @synthesize webView; -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSString *url = @https://api.weibo.com/oauth2/authorize?client_id=3693781153&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code&display=mobile; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; [self.webView setDelegate:self]; [self.webView loadRequest:request]; } -(void)viewDidLoad { [super viewDidLoad]; } #pragma mark - UIWebView Delegate Methods -(void)webViewDidFinishLoad:(UIWebView *)_webView { NSString *url = _webView.request.URL.absoluteString; NSLog(@absoluteString:%@,url); if ([url hasPrefix:@https://api.weibo.com/oauth2/default.html?]) { //找到”code=“的range NSRange rangeOne; rangeOne=[url rangeOfString:@code=]; //根據他“code=”的range確定code參數的值的range NSRange range = NSMakeRange(rangeOne.length+rangeOne.location, url.length-(rangeOne.length+rangeOne.location)); //獲取code值 NSString *codeString = [url substringWithRange:range]; NSLog(@code = :%@,codeString); //access token調用URL的string NSMutableString *muString = [[NSMutableString alloc] initWithString:@https://api.weibo.com/oauth2/access_token?client_id=3693781153&client_secret=7954135ee119b1fd068b8f41d2de5672&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code=]; [muString appendString:codeString]; NSLog(@access token url :%@,muString); //第一步,創建URL NSURL *urlstring = [NSURL URLWithString:muString]; //第二步,創建請求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlstring cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@POST];//設置請求方式為POST,默認為GET NSString *str = @type=focus-c;//設置參數 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; //第三步,連接服務器 NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; NSLog(@Back String :%@,str1); NSError *error; //如何從str1中獲取到access_token NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:&error]; NSString *_access_token = [dictionary objectForKey:@access_token]; NSLog(@access token is:%@,_access_token); } } @end
來幾張圖: