Oauth认证-新浪微博开发(下)

上篇文章主要针对新浪微博的授权过程中做了论述,接下来我们学习使用 oauth-signpost开源项目
进行 OAuth认证开发。  结合新浪微博的OAuth认证来说说具体的功能实现,首先罗列一下关键字组,下面四组关键字跟我们接下来OAuth认证有非常大的关系。
第一组:(App Key和App Secret),创 OAuthConsumer that should be used to sign the request
oauthVerifier
- NOTE: Only applies to service providers implementing OAuth 1.0a. Set to null if the service provider is still using OAuth 1.0. The verification code issued by the service provider after the the user has granted the consumer authorization. If the callback method provided in the previous step was OAuth.OUT_OF_BAND, then you must ask the user for this value. If your app has received a callback, the verfication code was passed as part of that request instead
protected String key)  Returns a single query parameter as served by the service provider in a token reply. You must call setResponseParameters(oauth.signpost.http.HttpParameters) with the set of parameters before using this method.
注意1:在Android中不要使用DefaultOAuth* 类,建议使用CommonHttpOAuth*,因为
since there's a bug in Android's java.net.HttpURLConnection that keeps it from working with some service providers.Instead, use the CommonsHttpOAuth* classes, since they are meant to be used with Apache Commons HTTP (that's what Android uses for HTTP anyway).


第一步,初始化httpOauthConsumer、httpOauthprovider对象。其中
httpOauthConsumer对象封装了请求用的AppKey和AppSecret值;httpOauthprovider对象提供相应操作。
第二步,执行httpOauthprovider.retrieveRequestToken(httpOauthConsumer, callBackUrl);执行这句,httpOauthConsumer将获取token和token_secret参数,并跳转到授权页面,并返回请求验证的URL

for(String s:httpOauthConsumer.getRequestParameters().keySet()){
Log.d("log", s+":"+httpOauthConsumer.getRequestParameters().getFirst(s));
}
输出的HttpOauthConsumer请求参数:
01-07 01:20:33.016: DEBUG/log(9592): oauth_callback:myapp://AuthorizeActivity
01-07 01:20:33.016: DEBUG/log(9592): oauth_consumer_key:30632531
01-07 01:20:33.016: DEBUG/log(9592): oauth_nonce:-8174893267510893022
01-07 01:20:33.026: DEBUG/log(9592): oauth_signature_method:HMAC-SHA1
01-07 01:20:33.026: DEBUG/log(9592): oauth_timestamp:1325899232
01-07 01:20:33.026: DEBUG/log(9592): oauth_version:1.0

此时输出
for(String s:httpOauthprovider.getResponseParameters().keySet()){
Log.d("log", s+":"+httpOauthprovider.getResponseParameters().getFirst(s));
}
显示为空。

可以这样理解,第一步只是向服务器发出来请求。

authorize_url:http://api.t.sina.com.cn/oauth/authorize?oauth_token=09e02bfbe399986bca8053a47be01082&oauth_callback=myapp://AuthorizeActivity

第三步,如果用户没有登录新浪微博,则会要求用户登录。否则将会出现一个页面,用户可以在此页面上一键同意或者拒绝对此应用授权。用户授权后,web应用页面将会重定向至你指定的oauth_callback,如果使用了callback,那么oauth_callback应该已经接到返回的信息,其中包含oauth_token和oauth_verifier

此时保存oauth_verifier参数,作为下次请求的参数。

Oauth_CallBack:myapp://AuthorizeActivity?oauth_token=8844ab4499c29d153508f837730b839f&oauth_verifier=226065

第四步,执行retrieveAccessToken(httpOauthConsumer,oauth_verifier)方法,获取授权的Token和Secret。

此时输出:
for(String s:httpOauthprovider.getResponseParameters().keySet()){
Log.d("log", s+":"+httpOauthprovider.getResponseParameters().getFirst(s));
}
返回参数:
01-07 01:30:58.876: DEBUG/log(11367): user_id:1571372175
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值