实现新浪微博Oauth认证
ViewController.h代码实现如下:
@interface ViewController : UIViewController<UIWebViewDelegate> @property (strong, nonatomic) UIWebView *OauthWebView; @end
ViewController.m文件实现如下:
- (void)viewDidLoad { [super viewDidLoad]; self.OauthWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 548)]; [self.view addSubview:self.OauthWebView]; //将API放入字符串中 NSString* urlStr = @"https://api.weibo.com/oauth2/authorize?client_id=3145625561&redirect_uri=http://www.baidu.com&display=mobile"; //将字符串转化为URL NSURL* url = [NSURL URLWithString:urlStr]; //创建请求对象 NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; self.OauthWebView.delegate = self; [self.OauthWebView loadRequest:request]; }
这时能够出现新浪微博的登陆界面:
我们现在已经能够调用Oauth认证页面了。接下来我们要获取code值。要用UIWebViewDelegate的代理方法
代码实现如下:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL* resultStr = [request URL]; NSString* tempStr = [resultStr absoluteString]; NSLog(@"%@",resultStr); //判断字符串中是否包含code= NSRange range = [tempStr rangeOfString:@"code="]; //如果不包含会返回为真,因此此处取反,就是说不为空的时候会为假,取反之后为真 if ( !(range.location == NSNotFound)) { //absoluteString作用是将NSURL类型转化为NSString NSString* tempStr = [resultStr absoluteString]; //componentsSeparatedByString NSArray* codeArr = [tempStr componentsSeparatedByString:@"="]; NSLog(@"%@",codeArr); NSString* code = [codeArr objectAtIndex:1]; NSLog(@"%@",code); //之后在此处下一行添加调用获取access_token值的方法即httpRequest:(NSString*)codeStr } return true; }
这时,我们就能够拿到code值,这个code值有什么用呢?就看新浪微博的api吧!
之后,我们要完成OAuth认证的最后一步,拿到access_token的值
在实现这个功能之前我们要导入ASIHTTP这个第三方库,怎么导入呢??自己百度吧 一搜一大把。
代码实现如下:
//获取access_token值的方法 -(void)httpRequest:(NSString*)codeStr { //3145625561 0a61cc8a017fa8ba6c98532fefa3c29c //http://www.baidu.com https://api.weibo.com/oauth2/access_token NSURL* urlStr = [[NSURL alloc] initWithString:@"https://api.weibo.com/oauth2/access_token"]; //post请求时要用ASIFormDataRequest类的对象 //我们要用到__block 就要先用__block来声明并且用到了ARC为了防止内存泄露 __block ASIFormDataRequest* requestBlock = [[ASIFormDataRequest alloc] initWithURL:urlStr] ; //就需要如下这种写法 __weak ASIFormDataRequest* request = requestBlock; //POST请求放参数的方法 [request setPostValue:@"3145625561" forKey:@"client_id"]; [request setPostValue:@"0a61cc8a017fa8ba6c98532fefa3c29c" forKey:@"client_secret"]; [request setPostValue:@"authorization_code" forKey:@"grant_type"]; [request setPostValue:codeStr forKey:@"code"]; [request setPostValue:@"http://www.baidu.com" forKey:@"redirect_uri"]; //设置代理 request.delegate = self; //startAsynchronous为异步请求 [request startAsynchronous]; [request setCompletionBlock:^{ NSLog(@"%@",[request responseString]); NSData* data = [request responseData]; NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString* access_tokenString = [dic objectForKey:@"access_token"]; NSLog(@"%@",access_tokenString); }]; }
要记得在
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
方法中添加 httpRequest方法的调用
这样我们就能够拿到access_token的值了。
OAuth认证到此完成简单吧!!
代码下载地址: "SinaOAuth认证.zip" http://vdisk.weibo.com/s/HrDOj
http://pan.baidu.com/share/link?shareid=3756342609&uk=3189484501