鏈接?
1.進(jìn)入微博開放平臺(tái)的微連接,創(chuàng)建應(yīng)用
2.獲取App Key:App Secret:
3.設(shè)置授權(quán)回調(diào)頁:取消授權(quán)回調(diào)頁:
代碼實(shí)現(xiàn)流程:
1.創(chuàng)建webView衷旅,設(shè)置代理疾就,實(shí)現(xiàn)協(xié)議方法
2.首先訪問授權(quán)接口Oauth2/authorize接口
https://api.weibo.com/oauth2/authorize?client_id=App Key&redirect_uri=回調(diào)頁面的網(wǎng)址&display=mobile
3.構(gòu)建請(qǐng)求
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.用戶登錄
在協(xié)議方法將要開始加載一個(gè)請(qǐng)求時(shí)
返回一個(gè)授權(quán)連接點(diǎn)擊授權(quán)
授權(quán)服務(wù)器返回一個(gè)連接連接中包含(redirect_uri+code)
輸出:http://www.new.com/?code=869f51c8f3dfd223824606808059fa50
請(qǐng)求授權(quán)完成后執(zhí)行shouldStartLoadWithRequest判斷是否返回code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"即將加載的請(qǐng)求路徑-- %@",request.URL.absoluteURL);
NSString *str = request.URL.absoluteString;
NSRange range = [str rangeOfString:@"?code"];
//發(fā)現(xiàn)str中有沒有code
if(range.location!=NSNotFound) {
//獲取到code
//拆分找code的碼
NSArray *array = [str componentsSeparatedByString:@"="];
NSString *code =[array lastObject];
NSLog(@"%@",code);
//創(chuàng)建請(qǐng)求管理類
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;
根據(jù)code構(gòu)建一次請(qǐng)求
請(qǐng)求結(jié)束方法中獲取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后面的部分存在的話,就進(jìn)入下面的方法
if(range.location != NSNotFound) {
//停止加載不用進(jìn)入回調(diào)頁面
[_webView stopLoading];
//拆分
NSArray *array = [str componentsSeparatedByString:@"="];
//拿出=后面的部分
NSString *code = [array lastObject];
//請(qǐng)求路徑
NSString *url =@"https://api.weibo.com/oauth2/access_token";
//封裝參數(shù)
NSDictionary *parma = @{@"client_id":kAPPKey, @"client_secret":kAPP_Secret, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kRedirect_uri};
//獲得網(wǎng)絡(luò)管理
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//設(shè)置適合AFNetWorking的響應(yīng)頭
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;
// ???????????}
//本地?cái)?shù)據(jù)持久化存儲(chǔ)數(shù)據(jù):方便后面取出使用
//1.屬性列表的方式plist文件輕量級(jí)的數(shù)據(jù)涉及到的主要的類NSUserDefaultsv
[[NSUserDefaults standardUserDefaults] setObject:[response ObjectobjectForKey:@"access_token"] forKey:@"access_token"];
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"error --- %@",error);
}];
}
return YES;
}