1.进入微博开放平台的微连接,创建应用
2.获取App Key:App Secret:
3.设置授权回调页:取消授权回调页:
代码实现流程:
1.创建webView,设置代理,实现协议方法
2.首先访问授权接口Oauth2/authorize接口
https://api.weibo.com/oauth2/authorize?client_id=App Key&redirect_uri=回调页面的网址&display=mobile
3.构建请求
webView加载
_webView= [[UIWebViewalloc]initWithFrame:self.view.bounds];
_webView.delegate=self;
[self.view addSubview:_webView];
NSURL*url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@&display=mobile",kAPPKey,kRedirect_uri]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
4.用户登录
在协议方法将要开始加载一个请求时
返回一个授权连接点击授权
授权服务器返回一个连接连接中包含(redirect_uri+code)
输出:http://www.new.com/?code=869f51c8f3dfd223824606808059fa50
请求授权完成后执行shouldStartLoadWithRequest判断是否返回code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"即将加载的请求路径-- %@",request.URL.absoluteURL);
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"?code"];
//发现str中有没有code
if(range.location!=NSNotFound) {
//获取到code
//拆分找code的码
NSArray *array = [str componentsSeparatedByString:@"="];
NSString *code =[array lastObject];
NSLog(@"%@",code);
//创建请求管理类
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlStr =@"https://api.weibo.com/oauth2/access_token";
NSDictionary *dic = @{@"client_id":kAPPKey, @"client_secret":kAPPSecret, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kRedirect_uri};
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/plain"];
[manager POST:urlStrparameters:dicconstructingBodyWithBlock:^(id_NonnullformData) {
}progress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
NSLog(@"responseObject ---> %@",responseObject);
[NSUserDefaults standardUserDefaults] setObject:<#(nullable id)#> forKey:<#(nonnull NSString *)#>
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"error -- %@",error);
}];
}
returnYES;
根据code构建一次请求
请求结束方法中获取access token
#import"ViewController.h"
#import"AFNetworking.h"
#define kAPPKey @"0000000000"
#define kAPP_Secret @"be7b7d29791adeca9000c7a174580d3e"
#define kRedirect_uri @"http://baidu.com"
@interfaceViewController(){
UIWebView *_webView;
}
@end
@implementationViewController
- (void)viewDidLoad {
[super viewDidLoad];
_webView= [[UIWebView alloc] initWithFrame:self.view.frame];
_webView.delegate = self;
[self.view addSubview:_webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@&display=mobile",kAPPKey,kRedirect_uri]]];
[_webView loadRequest:request];
/*
NSString *str =@"abcdefghijklmnopqrstuvwxyz";
//范围:o后面的部分
NSRange range = [strrangeOfString:@"o"];
NSString *last = [NSString stringWithFormat:@"%ld",range.location];
NSLog(@"%@",last);//14
//拆分
NSArray *arr = [str componentsSeparatedByString:@"o"];
NSLog(@"%@",[arr lastObject]);//pqrstuvwxyz
NSLog(@"%@",[arr firstObject]);//abcdefghijklmn
*/
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"---->%@",request.URL.absoluteURL);
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"?code"];
//?code后面的部分存在的话,就进入下面的方法
if(range.location != NSNotFound) {
//停止加载不用进入回调页面
[_webView stopLoading];
//拆分
NSArray *array = [str componentsSeparatedByString:@"="];
//拿出=后面的部分
NSString *code = [array lastObject];
//请求路径
NSString *url =@"https://api.weibo.com/oauth2/access_token";
//封装参数
NSDictionary *parma = @{@"client_id":kAPPKey, @"client_secret":kAPP_Secret, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kRedirect_uri};
//获得网络管理
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//设置适合AFNetWorking的响应头
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/plain"];
//连接
[manager POST:url parameters:parma progress:^(NSProgress *_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask *_Nonnulltask,id_NullableresponseObject) {
NSLog(@"responseObject ---> %@",responseObject);
// responseObject ---> {
// "access_token" = "2.00hMyWODWC__mD6a71a50f91CfpNwB";
// "expires_in" = 157670000;
// "remind_in" = 157670000;
// uid = 2963125123;
// }
//本地数据持久化存储数据:方便后面取出使用
//1.属性列表的方式plist文件轻量级的数据涉及到的主要的类NSUserDefaultsv
[[NSUserDefaults standardUserDefaults] setObject:[response ObjectobjectForKey:@"access_token"] forKey:@"access_token"];
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"error --- %@",error);
}];
}
return YES;
}