android author认证机制

package com.example.httpoauth;

public class OAuth {
 //创建一个客户端
 private CommonsHttpOAuthConsumer httpOauthConsumer;
 //创建一个服务器端
    private OAuthProvider httpOauthprovider;
    //创建密钥
    public String consumerKey;
    public String consumerSecret;
    //创建构造方法
    public OAuth()
    {   
        // 第一组:(App Key和App Secret)
        // 这组参数就是本系列文本第一篇提到的建一个新的应用获取App Key和App Secret。
        this("3315495489","e2731e7grf592c0fd7fea32406f86e1b");
    }
    public OAuth(String consumerKey,String consumerSecret)
    {
        this.consumerKey=consumerKey;
        this.consumerSecret=consumerSecret;
    }
   
    public Boolean RequestAccessToken(Activity activity,String callBackUrl){
        Boolean ret=false;
        try{
            httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
            httpOauthprovider = new DefaultOAuthProvider("http://api.t.sina.com.cn/oauth/request_token","http://api.t.sina.com.cn/oauth/access_token","http://api.t.sina.com.cn/oauth/authorize");
            String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, callBackUrl);
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
            ret=true;
        }catch(Exception e){
        }
        return ret;
    }
   
    public UserInfo GetAccessToken(Intent intent){
        UserInfo user=null;
        Uri uri = intent.getData();
        String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
        try {
            httpOauthprovider.setOAuth10a(true);
            httpOauthprovider.retrieveAccessToken(httpOauthConsumer,verifier);
        } catch (OAuthMessageSignerException ex) {
            ex.printStackTrace();
        } catch (OAuthNotAuthorizedException ex) {
            ex.printStackTrace();
        } catch (OAuthExpectationFailedException ex) {
            ex.printStackTrace();
        } catch (OAuthCommunicationException ex) {
            ex.printStackTrace();
        }
        SortedSet<String> user_id= httpOauthprovider.getResponseParameters().get("user_id");
        String userId=user_id.first();
        String userKey = httpOauthConsumer.getToken();
        String userSecret = httpOauthConsumer.getTokenSecret();
        user=new UserInfo();
        user.setUserId(userId);
        user.setToken(userKey);
        user.setTokenSecret(userSecret);
        return user;
    }
   
    public HttpResponse SignRequest(String token,String tokenSecret,String url,List params)
    {
        HttpPost post = new HttpPost(url);
        //HttpClient httpClient = null;
        try{
            post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
        }
        //关闭Expect:100-Continue握手
        //100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题
        post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        return SignRequest(token,tokenSecret,post);
    }
   
    public HttpResponse SignRequest(String token,String tokenSecret,HttpPost post){
        httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
        httpOauthConsumer.setTokenWithSecret(token,tokenSecret);
        HttpResponse response = null;
        try {
            httpOauthConsumer.sign(post);
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
        //取得HTTP response
        try {
            response = new DefaultHttpClient().execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值