当前位置:操作系统 > 安卓/Android >>

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);
    }
}
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,