android開發新浪微博-OAuth認證時報錯

來源:互聯網
上載者:User

首先上異常的:  報錯的主要原因是沒找到第三方的類。解決方案:項目右鍵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);    }} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.