标签:
一.OAuth新浪授权
新浪授权用户登录界面过程:
1.注册一个新浪微博的账号,称为新浪的开发者
2.登录新浪微博开发者首页 http://open.weibo.com/ 创建一个应用
3.填写应用名称 和 应用地址 https://www.baidu.com/
4.创建完成之后,会获得以下主要信息:1>.AppKey (应用的唯一标识):323532662
2>.AppSerect:227sdgdfshgfdhfj1348752
3>.RedirectURL(回调地址,有默认值):http:// (此为默认值)
5.重要的步骤见下面示意图步骤:
#import "OAuthViewController.h"
#import "AFNetworking.h"
@interface OAuthViewController ()<UIWebViewDelegate>
@end
@implementation OAuthViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
通过webview进行OAuth新浪授权请求
*/
//1.创建一个webview
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = self.view.bounds;
webView.delegate = self;
[self.view addSubview:webView];
//2.用webView加载登陆页面(新浪提供的)
/* 请求参数:
client_id true string 申请应用时分配的AppKey。
redirect_uri true string 授权回调地址,站外应用需与设置的回调地址一致,站内应用需填写canvas page的地址。
*/
NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3125184858&redirect_uri=http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
#pragma mark ---webView代理方法 用于验证请求是否发出
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"---webViewDidFinishLoad");
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webViewDidStartLoad");
}
//开始网络请求
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//1.获得url
NSString *url = request.URL.absoluteString;
//2.判断是否为回调地址 截取code的参数值
NSRange range = [url rangeOfString:@"code="];
if (range.length != 0) {
//用于获得请求参数后面的code 位置加上当前位置可以获得所需参数首位的地址
int fromIndex = range.location + range.length;
NSString *code = [url substringFromIndex:fromIndex];
//利用code 换取一个accessToKen
[self accessToKenWithCode:code];
//禁止加载回调地址
return NO;
}
return YES;
}
/*
利用code(授权成功后的request token)换取一个accessToken
@param code 授权成功后的request token
*/
-(void)accessToKenWithCode:(NSString *)code
{
/*
URL:https://api.weibo.com/oauth2/access_token
请求参数:
client_id:申请应用时分配的AppKey
client_secret:申请应用时分配的AppSecret
grant_type:使用authorization_code
redirect_uri:授权成功后的回调地址
code:授权成功后返回的code
*/
//1.请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
//2.拼接请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"client_id"] = @"3125184858";
params[@"client_secret"] = @"5fbe16f31ea2c34f3f0be9a6185cdd04";
params[@"grant_type"] = @"authorization_code";
//默认请求成功后的回调地址
params[@"redirect_uri"] = @"http://www.baidu.com";
params[@"code"] = code;
//3.发送请求
[mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
NSLog(@"请求成功 -- %@",responseObject);
//沙盒路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"/account.plist"];
//将返回的账号数据 存进沙盒
[responseObject writeToFile:path atomically:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"请求失败--%@",error);
}];
}
@end
标签:
原文地址:http://www.cnblogs.com/erdeng/p/4890130.html