很久没写博客,最近在搞一个新浪微博的第三方应用,涉及到了oauth2.0授权获取access_token,特此记录分享! 步骤一:添加应用 进入新浪微博开放平台(没有的话自行注册),进入“管理中心“,点击”创建应用”,选择“微链接应用”,再点击“创建应用”,,
很久没写博客,最近在搞一个新浪微博的第三方应用,涉及到了oauth2.0授权获取access_token,特此记录分享!
步骤一:添加应用
进入新浪微博开放平台(没有的话自行注册),进入“管理中心“,点击”创建应用”,选择“微链接应用”,再点击“创建应用”,,选“移动应用”,填写相应的信息,其中应用地址没有的话可随便,勾选平台后提交。注意保存你的app key和app secret以备后用。
步骤二:oauth2.0授权设置
应用创建完后可以在“管理中心”-“我的应用”中查看信息,在“应用信息”--“高级信息”中可以设置网站的授权回调页和取消授权回调页。授权回调页会在用户授权成功后会被回调,同时传回一个“code”参数,开发者可以用code换取access_token值。当然如果是移动应用,比如本文是没有自己授权回调页的,建议这里填:https://api.weibo.com/oauth2/default.html 或者 http://www.baidu.com 之类的。如果授权后传回的形式如下:
https://api.weibo.com/oauth2/default.html?code=a6146547f981199c07348837b0629d5d
我们只要获取其中code的值a6146547f981199c07348837b0629d5d即可,注意code的值每次都是不一样的。
步骤三:引导用户授权
引导需要授权的用户到如下页面:
https://api.weibo.com/oauth2/authorize?client_id=your_client_id&response_type=code&redirect_uri=your_registered_redirect_uri
your_client_id:即应用的appkey,可以在应用基本信息里查看到。
your_registered_redirect_uri:即之前填写的授权回调页,注意一定要和你在开发平台填写的完全相同,这里以https://api.weibo.com/oauth2/default.html 为例。
如果用户授权成功后,会跳转到回调页,开发者此时需要得到url参数中的code值,注意code只能使用一次。
步骤四:换取access token
开发者可以访问如下页面得到access token:
https://api.weibo.com/oauth2/access_token?client_id=your_client_id&client_secret=your_client_secret&grant_type=authorization_code&redirect_uri=your_registered_redirect_uri&code=code
your_client_id:即应用的appkey,可以在应用基本信息里查看到。
your_client_secret:即应用的app secret,可以在应用基本信息里查看到。
your_registered_redirect_uri:即之前填写的授权回调页
code:就是步骤三引导用户授权后回传的code。
如果都没有问题,就可以得到access token了,返回示例:
{
access_token: access_token,
expires_in: 1234,
remind_in:798114,
uid:12341234
}
最后做了一个xcode 5.0 storyboard的demo,用到一个uiviewcontroller和一个uiwebview。
看代码如下:
#import @interface oauthwebviewcontroller : uiviewcontroller@property (weak, nonatomic) iboutlet uiwebview *webview;@end
#import oauthwebviewcontroller.h@implementation oauthwebviewcontroller@synthesize webview;-(void)viewwillappear:(bool)animated{ [super viewwillappear:animated]; nsstring *url = @https://api.weibo.com/oauth2/authorize?client_id=3693781153&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code&display=mobile; nsurlrequest *request = [[nsurlrequest alloc] initwithurl:[nsurl urlwithstring:url]]; [self.webview setdelegate:self]; [self.webview loadrequest:request];}-(void)viewdidload{ [super viewdidload];}#pragma mark - uiwebview delegate methods-(void)webviewdidfinishload:(uiwebview *)_webview{ nsstring *url = _webview.request.url.absolutestring; nslog(@absolutestring:%@,url); if ([url hasprefix:@https://api.weibo.com/oauth2/default.html?]) { //找到”code=“的range nsrange rangeone; rangeone=[url rangeofstring:@code=]; //根据他“code=”的range确定code参数的值的range nsrange range = nsmakerange(rangeone.length+rangeone.location, url.length-(rangeone.length+rangeone.location)); //获取code值 nsstring *codestring = [url substringwithrange:range]; nslog(@code = :%@,codestring); //access token调用url的string nsmutablestring *mustring = [[nsmutablestring alloc] initwithstring:@https://api.weibo.com/oauth2/access_token?client_id=3693781153&client_secret=7954135ee119b1fd068b8f41d2de5672&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code=]; [mustring appendstring:codestring]; nslog(@access token url :%@,mustring); //第一步,创建url nsurl *urlstring = [nsurl urlwithstring:mustring]; //第二步,创建请求 nsmutableurlrequest *request = [[nsmutableurlrequest alloc]initwithurl:urlstring cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10]; [request sethttpmethod:@post];//设置请求方式为post,默认为get nsstring *str = @type=focus-c;//设置参数 nsdata *data = [str datausingencoding:nsutf8stringencoding]; [request sethttpbody:data]; //第三步,连接服务器 nsdata *received = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil]; nsstring *str1 = [[nsstring alloc]initwithdata:received encoding:nsutf8stringencoding]; nslog(@back string :%@,str1); nserror *error; //如何从str1中获取到access_token nsdictionary *dictionary = [nsjsonserialization jsonobjectwithdata:received options:nsjsonreadingmutablecontainers error:&error]; nsstring *_access_token = [dictionary objectforkey:@access_token]; nslog(@access token is:%@,_access_token); }}@end
来几张图:
demo下载地址:http://download.csdn.net/detail/wangqiuyun/6851621
注意替换为你的appkey和app secret。