微博开放平台: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;
}
}