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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

#import"ViewController.h"

#import"AFNetworking.h"@interfaceViewController ()<UIWebViewDelegate>@end@implementationViewController- (void)viewDidLoad {

[super viewDidLoad];//1.使用webviewUIWebView *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=29a7addea0226eeaf94768440639189fNSInteger 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:paramssuccess:^(NSURLSessionDataTask *task,idresponSEObject) {

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,idresponseObject) {

NSLog(@"成功返回的地址是 : %@",responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {

NSLog(@"返回失败.");

}];//2.00aE6vVD6TADXCd89cbb1e870CqJn_} failure:^(NSURLSessionDataTask *task, NSError *error) {

NSLog(@"token 请求失败.");

}];//阻止当前webviwe跳转到回调页面.returnNO;

}returnYES;

}@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航