【问题标题】:Error in oAuth for Twitter for AndroidAndroid 版 Twitter 的 oAuth 错误
【发布时间】:2012-01-01 12:26:46
【问题描述】:

我正在使用应用程序通过 Twitter4j 库授权 Twitter 中的用户。我想加入一个功能,我的移动应用程序打开。它有一个登录按钮,点击后会出现 Twitter 登录对话框,让您输入登录信息。登录完成后,将打开另一个屏幕。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class AndTweetVJActivity extends Activity {
    /** Called when the activity is first created. */


    Twitter twitter;
    RequestToken requestToken;
    public final static String consumerKey = "myKey"; // "your key here";
    public final static String consumerSecret = "myKey"; // "your secret key here";
    private final String CALLBACKURL = "T4JOAuth://main";  //Callback URL that tells the WebView to load this activity when it finishes with twitter.com. (see manifest)


        //Calls the OAuth login method as soon as its started
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            OAuthLogin();
        }

        /* Creates object of Twitter and sets consumerKey and consumerSecret
         * - Prepares the URL accordingly and opens the WebView for the user to provide sign-in details
         * - When user finishes signing-in, WebView opens your activity back  */

        void OAuthLogin() {
            try {
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
                String authUrl = requestToken.getAuthenticationURL();
                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                        .parse(authUrl)));
            } catch (TwitterException ex) {
                Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
                Log.e("in Main.OAuthLogin", ex.getMessage());
            }
        }


        /*
         * - Called when WebView calls your activity back.(This happens when the user has finished signing in)
         * - Extracts the verifier from the URI received
         * - Extracts the token and secret from the URL 
         */
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            Uri uri = intent.getData();
            try {
                String verifier = uri.getQueryParameter("oauth_verifier");
                AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,verifier);
                String token = accessToken.getToken(), secret = accessToken.getTokenSecret();
                //displayTimeLine(token, secret); //after everything, display the first tweet 

            } catch (TwitterException ex) {
                Log.e("Main.onNewIntent", "" + ex.getMessage());
            }

        }
    }

但是在运行这个应用程序时,它在 logcat 中给了我错误:

11-18 10:36:27.727: E/in Main.OAuthLogin(282): 401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <?xml version="1.0" encoding="UTF-8"?>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <hash>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <error>Desktop applications only support the oauth_callback value 'oob'</error>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <request>/oauth/request_token</request>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): </hash>

我相信我没有设置回调 URL,但我也这样做了 https://dev.twitter.com/pages/welcome-anywhere 在我的应用中。

【问题讨论】:

标签: java android twitter oauth twitter4j


【解决方案1】:

确保您没有在 twitter 应用程序注册时将您的应用程序注册到桌面应用程序类别中。

【讨论】:

  • 现在没有这样的选项。我所能做的就是用回调 url 注册我的应用程序,以确保它是一个网络应用程序。如果回调不在那里,它是一个使用 oob 而不是 oAuth 的桌面应用程序
【解决方案2】:

我的错误...我的 Web 服务器已关闭,直到 5 分钟前我才注意到它。感谢所有建议。我已经做过这些事情了。

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2016-08-16
    • 2014-06-01
    • 2015-11-06
    • 2014-02-11
    • 1970-01-01
    相关资源
    最近更新 更多