标签:
套路;
1.创建app http://open.weibo.com/apps/3989606836/info/advanced
2. 拿到app的 有关信息 ,查看微博api
3, http://open.weibo.com/wiki/Oauth2/authorize OAuth2的authorize接口,
4. http://open.weibo.com/wiki/OAuth2/access_token 获取 access_token
6,可以使用access_token,,作为参数进行微博的网络请求
相关代码
#import <UIKit/UIKit.h> @interface IWOAuthViewController : UIViewController @end
#import "IWOAuthViewController.h" #import "MBProgressHUD+MJ.h" #import "AFNetworking.h" #import "MJExtension.h" #import "IWAccount.h" #import "IWAccountTool.h" #import "HomeViewController.h" @interface IWOAuthViewController ()<UIWebViewDelegate> @end @implementation IWOAuthViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIWebView *webView = [[UIWebView alloc] init]; webView.frame = self.view.bounds; webView.delegate = self; [self.view addSubview:webView]; self.view.backgroundColor = [UIColor orangeColor]; //PiersNovia;3989606836;0300642b574cdcd130f008bf3500f20b;http://www.baidu.com //加载页面,这里加载失败的话需要修改plist文件 NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3989606836&redirect_uri=http://www.baidu.com"]; NSURLRequest *requset = [NSURLRequest requestWithURL:url]; [webView loadRequest:requset]; } #pragma mark - webView回调方法 - (void)webViewDidStartLoad:(UIWebView *)webView { // 显示提醒框 [MBProgressHUD showMessage:@"哥正在帮你加载中..."]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { // 隐藏提醒框 [MBProgressHUD hideHUD]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { // 隐藏提醒框 [MBProgressHUD hideHUD]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { //每一次网络请求都会返回一个request,我们要拿到一个code NSString *urlStr = request.URL.absoluteString; // should start == http://www.baidu.com/?code=9d3115d76eb1ab915f391514ceb20dbd NSRange range = [urlStr rangeOfString:@"code="]; if (range.length) { //0. 拿到code NSString *code = [urlStr substringFromIndex:range.location+range.length]; // 1.创建请求管理对象 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // 2.封装请求参数 NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"client_id"] = @"3989606836"; params[@"client_secret"] = @"0300642b574cdcd130f008bf3500f20b"; params[@"grant_type"] = @"authorization_code"; params[@"code"] = code; params[@"redirect_uri"] = @"http://www.baidu.com"; // 3.发送请求 [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { //解析文件形成模型 IWAccount *account =[IWAccount objectWithKeyValues:responseObject]; //存储模型 [IWAccountTool saveAccount:account]; //跳转 [self presentViewController:[HomeViewController new] animated:YES completion:nil]; [MBProgressHUD hideHUD]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 隐藏提醒框 [MBProgressHUD hideHUD]; }]; [MBProgressHUD hideHUD]; } return YES; }
标签:
原文地址:http://www.cnblogs.com/coderMJL/p/4852301.html