新浪Oauth2.0認證

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()<UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self;
    [self.view addSubview:webView];
    
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=500210150&redirect_uri=http://www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

//可以改變request
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSLog(@"%@",request.URL.absoluteString);
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"code="];
    if (range.length != 0) {
        NSInteger index = range.location + range.length;
        NSString *code = [url substringFromIndex:index];
        
        NSString *urlStr = @"https://api.weibo.com/oauth2/access_token";
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSDictionary *paramaDic = @{
                                    @"client_id":@"500210150",
                                    @"client_secret":@"9414dad23cbef8b45c2be459f5bbe4b3",
                                    @"grant_type":@"authorization_code",
                                    @"code":code,
                                    @"redirect_uri":@"http://www.baidu.com"
                                    };
        [manager POST:url parameters:paramaDic success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"%@",responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
         }
    return YES;
}
@end

運行結(jié)果:

運行后進入的登錄界面
有個授權(quán)過程后進入回調(diào)頁面
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容