新浪Oauth2.0认证

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

@interface ViewController ()

@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

运行结果:

新浪Oauth2.0认证_第1张图片
运行后进入的登录界面
新浪Oauth2.0认证_第2张图片
有个授权过程后进入回调页面

你可能感兴趣的:(新浪Oauth2.0认证)