码迷,mamicode.com
首页 > 移动开发 > 详细

iOS新浪微博OAuth2.0认证代码

时间:2015-11-19 14:58:10      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:


#import
"ViewController.h"
#import "AFNetworking.h" @interface ViewController ()<UIWebViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1.使用webview UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:webView]; //要让webView发起请求 我需要一个新浪的登录页面 //接口 https://api.weibo.com/oauth2/authorize 这个接口. //使用那个平台,就要使用哪个平台的sdk 只需要提供三个参数 app key ,app sercect?,回调页网址. /** client_id 申请应用时分配的AppKey。 redirect_uri 授权回调地址,站外应用需与设置的回调地址一致,站内应用需填写canvas page的地址。 */ NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=2320601559&redirect_uri=http://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; webView.delegate = self; [webView loadRequest:request]; //当用户点击授权的时候,新浪服务器返回一个重定向地址,并且绑定了一个重要的参数 code //code是干嘛的?,demo应用要找到这个code 然后用这个code去新浪服务器去请求toke } - (void)webViewDidStartLoad:(UIWebView *)webView { NSString *url = webView.request.URL.absoluteString; NSLog(@"webview开始加载,加载地址是 : %@",url); } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"webView加载结束"); } - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //判断一下请求是否是服务器返回的重定向请求? NSString *url = request.URL.absoluteString; //是否包含code= NSRange range = [url rangeOfString:@"code="]; if (range.length != 0) { //这里就要截取code=后面的数据. // http://www.baidu.com/?code=29a7addea0226eeaf94768440639189f NSInteger index = range.location + range.length; NSString *code = [url substringFromIndex:index]; //自己发起一个请求获取access_token /** client_id string 申请应用时分配的AppKey。 client_secret string 申请应用时分配的AppSecret。 grant_type string 请求的类型,填写authorization_code grant_type为authorization_code时 必选 类型及范围 说明 code string 调用authorize获得的code值。 redirect_uri string 回调地址,需需与注册应用里的回调地址一致。 */ AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager]; NSString *tokenURL = @"https://api.weibo.com/oauth2/access_token"; NSDictionary *params = @{ @"client_id":@"2320601559", @"client_secret":@"d77590e7cb2b4dda50e5c17359380d0e", @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":@"http://www.baidu.com" }; [sessionManager POST:tokenURL parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"token 请求成功 %@",responseObject); // 2.00aE6vVD6TADXCd89cbb1e870CqJn_ NSDictionary *result = (NSDictionary *)responseObject; NSString *token = result[@"access_token"]; [sessionManager GET:@"https://api.weibo.com/2/statuses/public_timeline.json" parameters:@{@"access_token":token} success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"成功返回的地址是 : %@",responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"返回失败."); }]; // 2.00aE6vVD6TADXCd89cbb1e870CqJn_ } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"token 请求失败."); }]; //阻止当前webviwe跳转到回调页面. return NO; } return YES; } @end

 

iOS新浪微博OAuth2.0认证代码

标签:

原文地址:http://www.cnblogs.com/allencelee/p/4977408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!