0

我正在尝试从这个 URL http://androidcodeexamples.blogspot.in/2011/12/how-to-integrate-twitter-in-android.html为我的 android 应用程序集成 Twitter 登录

它似乎登录了,但不知何故无法生成访问令牌我现在相信的是,也许 twitter 已经修改了它的访问令牌 url......

private static final String TWITTER_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token";
private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize";
private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token";

那么任何人都可以帮助设置访问令牌的网址吗

4

2 回答 2

0

您链接中的代码非常复杂。试试我博客中的这个工作示例http://daiwei.lu/2014/01/22/twitter-oauth-flow-on-android/

它使用twitter4jandWebView来获取用户授权,然后获取访问令牌并在最后将其记录到控制台。

于 2014-01-23T22:10:26.433 回答
0

试试看

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
            case HIDE_PROGRESS_DIALOG:{
                hideProgressDialog();
            }break;
            case PROCESS_TOKEN:{
                processToken(url);
            }break;
        }
    }
}

@Override
public void create() {
    LinearLayout mainLayout = new LinearLayout(this);
    mainLayout.setOrientation(LinearLayout.VERTICAL);
    mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT, 
            LinearLayout.LayoutParams.FILL_PARENT);
    mainLayout.setLayoutParams(lp);

    webView = new WebView(this);
    webView.setLayoutParams(lp);

    mainLayout.addView(webView);

    setContentView(mainLayout);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:{
                }break;    
                case MotionEvent.ACTION_UP:{
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                }break;
            }
            return false;
        }
    });
    webView.setWebViewClient(new WebViewClient(){

        @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.startsWith(CALLBACK_URL)) {
                TwitterAuthorizationActivity.this.url = url;

                mHandler.sendEmptyMessage(PROCESS_TOKEN);

                return true;
        }else if (url.startsWith("authorize")) {
                return false;
        }

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
        }



    });

    mTwitter = new TwitterFactory().getInstance();

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey);
    mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token",
            "https://twitter.com/oauth/access_token",
            "https://twitter.com/oauth/authorize");

    try{
        authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
    }catch(OAuthCommunicationException oACEx){
        Log.d("", "");
    }catch(OAuthMessageSignerException oAMSEx){
        Log.d("", "");
    }catch(OAuthNotAuthorizedException oANAEx){
        Log.d("", "");
    }catch(OAuthExpectationFailedException oAEFEx){
        Log.d("", "");
    }

    webView.loadUrl(authUrl);
}

public void processToken(String callbackUrl)  {
    final String verifier = getVerifier(callbackUrl);

    int what = 1;

    try {
        mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);

        mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());

        mTwitter.setOAuthConsumer(twitterConsumerKey, twitterSecretKey);

        mTwitter.setOAuthAccessToken(mAccessToken);

        what = 0;
    } catch (Exception e){
    }
}

private String getVerifier(String callbackUrl) {
    String verifier  = "";

    try {
        callbackUrl = callbackUrl.replace("twitterapp", "http");

        URL url = new URL(callbackUrl);
        String query = url.getQuery();

        String array[] = query.split("&");

        for (String parameter : array) {
            String v[] = parameter.split("=");

            if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
                verifier = URLDecoder.decode(v[1]);
                break;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return verifier;
}

此解决方案使用Twitter4J库。

于 2014-01-22T09:57:06.833 回答