android开发新浪微博-OAuth认证时报错

首先上异常的截图:

 

报错的主要原因是没找到第三方的类。解决方法:项目右键Properties ------->Build Path------->Order And Export将第三方的jar包打勾。然后Clear自己的项目重新编译运行解决上述问题。详细请看截图

操作完上述步骤后Clear项目然后编译运行就OK了。

 

 

顺便把新浪OAuth认证源码给贴出来需要的哥们下载看看:

  认证需要新浪的jar包。signpost-commonshttp4-1.2.1.1.jar  signpost-core-1.2.1.1.jar下载地址:jar包下载

        ===================文件名:AuthActivity.java===================
package OAuth4Sina.com;

import java.util.SortedSet;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 
 * <p>Title: AuthActivity.java</p>
 * <p>Description: OAuth认证的实现</p>
 * <p>Copyright: Copyright (c) 2011</p>
 * <p>Company: CTX Teachnology</p>
 * <p>CreateTime: 2012-12-22 下午04:21:04</p>
 * 
 * @author YGC
 * @version V1.0
 * @since JDK1.6
 */

public class AuthActivity extends Activity {
    CommonsHttpOAuthConsumer httpOauthConsumer;
    OAuthProvider httpOauthprovider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn2 = (Button) findViewById(R.id.btn);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    String consumerKey = "434313181";//替换成自己应用的key
                    String consumerSecret = "458afde9c2ad61e9690296d8cca6488b";//也是自己的Secret
                    String callBackUrl = "myapp://AuthActivity";
                    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);
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse(authUrl)));
                    int b = 4;
                } catch (Exception e) {
                    String s = e.getMessage();
                }
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        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();

        TextView text = (TextView) findViewById(R.id.text);
        text.setText("suerId:" + userId + "/userKey:" + userKey
                + "/userSecret:" + userSecret);
    }
}

 

源码地址:源码下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值