很久没写博客,最近在搞一个新浪微博的第三方应用,涉及到了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 <uikit> @interface OAuthWebViewController : UIViewController<uiwebviewdelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @end</uiwebviewdelegate></uikit>
#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。

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

查看InMysqlareBeneForsImplifyingComplexqueries,增强安全性,确保dataConsistency,andOptimizingPerformance.1)他们simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。