新浪授权认证(不用SDK)
微博开放平台:http://open.weibo.com/
微博开放接口的调用,如发微博、关注等,都是需要获取用户身份认证的。目前微博开放平台用户身份鉴权主要采用的是OAuth2.0。另外,为了方便开发者开发、测试自己的应用,我们还提供了Basic Auth的身份鉴权方式,但Basic Auth仅适用于应用所属的开发者自己调用接口。
1.获取新浪的登录页面(UIWebView)
2.用户输入得到状态码(code)
3.用code换令牌 Token
#import "ViewController.h"
#define kAppKey @""微博注册应用之后得到
#define kAppSecret @""微博注册应用之后得到
#define kRedirect_url @"https://www.baidu.com"
#define kAccessTokenKey @"kAccessTokenKey"
#define kExpiresTime @"kExpiresTime"
#define kUserID @"kUserID"
@interface ViewController ()<UIWebViewDelegate>
@property (strong,nonatomic) UIWebView * webView;
@end
@implementation ViewController
- (IBAction)Login:(UIButton *)sender {
//1.使用oauth2/authorize发起请求
//拼接网址
NSString * urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",kAppKey,kRedirect_url];
NSURL * url = [NSURL URLWithString:urlString];
//创建请求
NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
//创建页面
self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
_webView.delegate = self;
[self.view addSubview:_webView];
//加载
[_webView loadRequest:request];
}
- (IBAction)Logout:(UIButton *)sender {
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:nil forKey:kAccessTokenKey];
[userDefault setObject:nil forKey:kExpiresTime];
[userDefault setObject:nil forKey:kUserID];
[userDefault synchronize];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//return YES 加载 return NO 不加载
//2.获取code状态码
NSRange range = [[request.URL relativeString]rangeOfString:@"code="];
if (range.length != 0 ) {
//解析code
NSString * code = [[request.URL relativeString]substringFromIndex:range.location+range.length];//截取出来
[self.webView removeFromSuperview];//获取到就不需要了
NSLog(@"%@",code);
//3.换取令牌
//发送Post请求
NSURL * url =[NSURL URLWithString:@"https://api.weibo.com/oauth2/access_token"];
NSMutableURLRequest * upRequest = [NSMutableURLRequest requestWithURL:url];//用POST而不是用GET
[upRequest setHTTPMethod:@"POST"];
NSURLSession * session =[NSURLSession sharedSession];
NSString * dataString = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=authorization_code&code=%@&redirect_uri=%@&",kAppKey,kAppSecret,code,kRedirect_url];
NSData * updata = [dataString dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionUploadTask * upLoadTask = [session uploadTaskWithRequest:upRequest fromData:updata completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//用字典封装json数据
NSLog(@"%@",dataDic);
//我们要用的---access_token expires_in uid
NSString * accesstoken = [dataDic objectForKey:@"access_token"];
NSString * expiresin = [dataDic objectForKey:@"expires_in"];
NSString * uid = [dataDic objectForKey:@"uid"];
//保存下来
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:accesstoken forKey:kAccessTokenKey];
[userDefault setObject:expiresin forKey:kExpiresTime];
[userDefault setObject:uid forKey:kUserID];
[userDefault synchronize];
}];
[upLoadTask resume];
return NO;
}else{
return YES;
}
}
最新文章
- 删除docker的运行目录出错
- C语言用面向对象的思想写贪吃蛇
- DOS终端不能使用JAVA命令
- 软件工程随堂作业--随机产生30到四则运算(c语言)
- boosting和bagging
- ul动态增加li
- Myeclipse+Tomcat安装与配置
- VC++ WIN32 sdk实现按钮自绘详解 之二(关键是BS_OWNERDRAW和WM_DRAWITEM)
- APUE 1 -- Unix数据结构
- linux下nginx【反向代理】配置【负载均衡】配置
- 关于阿里云Centos7 Mailx发送邮件失败的处理
- 使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试
- jenkins 邮箱设置
- SPOJ705-New Distinct Substrings-后缀数组
- python高并发的解决方案
- 安全系列之CSRF初探
- 键盘按键js效果
- 2018.11.02 NOIP模拟 距离(斜率优化dp)
- 利用requestAnimationFrame和Tween算法实现兼容所有浏览器的运动动画,直接秒杀Css3动画
- Android笔记之开机自启