4

这是我的代码,我不断收到异常“授权失败(服务器回复 401)。如果消费者密钥不正确或签名不匹配,则可能发生这种情况。” 在这条线上'provider.retrieveAccessToken(consumer, verifier);'。我已经三次检查了我的消费者密钥和秘密,我的 twitter 应用程序设置为浏览器并尝试设置 provider.setOAuth10a(true),我已经为此苦苦挣扎了 2 天!!我正在使用路标 1.2.1.1(核心和 commonshttp4),如果有人可以提供帮助!请让我绝望

    private static final String CONSUMER_KEY = "MY_CONSUMER_KEY";
    private static final String CONSUMER_SECRET = "MY_CONSUMER_SECRET";

    private static final String CALLBACK_URL = "tweet-mapper://mainactivity";

    private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
    private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
    private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize";

    private static final String PREFERENCE_FILE = "twitter_oauth.prefs";

    private static CommonsHttpOAuthConsumer consumer;
    private static CommonsHttpOAuthProvider provider;

    private static String ACCESS_KEY;
    private static String ACCESS_SECRET;

    private Twitter twitter;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        loginViaOAuth();

    }

    private void loginViaOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider.setOAuth10a(true);
            provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
            String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();

        Uri uri = this.getIntent().getData();
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
            Log.d("verifier:", verifier);
            try {

                provider.setOAuth10a(true);
                provider.retrieveAccessToken(consumer, verifier);
                ACCESS_KEY = consumer.getToken();
                ACCESS_SECRET = consumer.getTokenSecret();

                AccessToken a = new AccessToken(ACCESS_KEY, ACCESS_SECRET);

                // initialize Twitter4J
                twitter = new Twitter();
                twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                twitter.setOAuthAccessToken(a);
                String tweet = "#OAuth working via android app!";

                twitter.updateStatus(tweet);
                Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
4

3 回答 3

1

刚刚找到了一个可能的解决方案:您需要在您的 twitter 应用程序帐户上设置一个回调 URL。

于 2012-03-06T16:06:36.300 回答
0

I had exactly the same problem on my Android application. It was even more frustrating that my twitter login was perfectly working and starting to fail on the signature for some random reasons.

I ran a lot of tests and I found that the problem came from the Android browser which is used in the OAuth process:

  • if you are logging in using the stored login/password, or if you have a cookie with your Twitter session and just have to click on "Accept", it fill fail with the 401 error
  • if you manually delete and re-enter your password, then it works!

I still can't understand how this affects the API call, but I guess there is some mix up in the browser when you submit the "accept" form with pre-entered information.

I'd be very curious to see if my workaround solves also your problem. I understand this is not a proper solution, but this is a beginning.

EDIT: use http:// instead of https:// for the Twitter OAuth URLs and it solves the problem. I still don't unsertand what is happening...

于 2011-04-04T13:12:33.177 回答
0

检查您的 api 请求,它必须是 .json 或 .xml,例如 https://api.jabbakam.com/network/get_list.jsonhttp://api.twitter.com/1/account/verify_credentials.xml

我建议你使用 Scribe 库,这里有一个用于使用 Twitter API 的内置类。

https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/TwitterExample.java

当你创建你的 twitter 密钥时,你是否设置了访问级别:读写?

于 2012-07-24T10:59:35.673 回答