Question

i am trying to get twitter work.

Error which i receive is: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match

I have already checked a lot of same issues here, on stackoverflow and here what i already tried: 1) checked consumer key (it is the same with that on dev.twitter.com) 2) added Callback URL for my app on dev.twitter.com 3) updated library to twitter-4j-core-3.0.5.jar 4) checked if time of my tablet is correct (set Eastern European Time)

Also i must say that some month ago Twitter in application worked properly. Then somehow it broke down.

Here is my code:

class GetOAuthVerifierTask extends AsyncTask<Void, Void, String> {
    private Context context;

    public GetOAuthVerifierTask(Context context) {
        this.context = context;
        dialog = ProgressDialog.show(TwitterActivity.this, getString(CANNOT_GET_REQUEST_TOKEN), null);
    }

    @Override
    protected String doInBackground(Void... params) {
        TwitterUtils twitterUtils = TwitterUtils.getInstance();

        OAuthConsumer consumer = twitterUtils.createConsumer();
        OAuthProvider provider = twitterUtils.createProvider();
        try {
            final String url = provider.retrieveRequestToken(consumer,
                            twitterUtils.getCallbackURL(context));
            twitterUtils.setConsumerToken(context, consumer.getToken());
            twitterUtils.setConsumerSekretToken(context, consumer.getTokenSecret());

            return url;
        } catch (Exception e) {
            Logger.debug("Can not retrieve request token");
            Logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(String url) {
        dialog.dismiss();
        if (url != null){
             // HERE IT WORKS CORRECT
             web.loadUrl(url);
        }

        else{
            Toast.makeText(TwitterActivity.this, getString(DOWNLOAD_WAIT_MESSAGE),
                    Toast.LENGTH_LONG).show();
        }

    }
}

class GetAccessTokenTask extends AsyncTask<Uri, Void, Boolean> {
    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(TwitterActivity.this, getString(CANNOT_GET_REQUEST_TOKEN), null);
    }

    @Override
    protected Boolean doInBackground(Uri... params) {
        TwitterUtils twitterUtils = TwitterUtils.getInstance();

        String oauthVerifier = params[0].getQueryParameter(OAuth.OAUTH_VERIFIER);

        OAuthConsumer consumer = twitterUtils.createConsumer();
        consumer.setTokenWithSecret(twitterUtils.getConsumerToken(TwitterActivity.this),
                        twitterUtils.getConsumerSekretToken(TwitterActivity.this));
        OAuthProvider provider = twitterUtils.createProvider();
        try {
            provider.retrieveAccessToken(consumer, oauthVerifier);

            twitterUtils.setAccessToken(TwitterActivity.this, consumer.getToken());
            twitterUtils.setAccessTokenSecret(TwitterActivity.this, consumer.getTokenSecret());
        } catch (Exception e) {
            Logger.debug("Can not retrieve access token");
            Logger.error(e.getMessage(), e);
            return false;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        dialog.dismiss();
        if (result) {
            TwitterActivity.this.sendMessage();
            TwitterActivity.this.finish();
        } else {
            // HERE I GET 401
            Toast.makeText(TwitterActivity.this, getString(DOWNLOAD_WAIT_MESSAGE),
                            Toast.LENGTH_LONG).show();
        }
    }
}
Was it helpful?

Solution

Just found the solution: i added line provider.setOAuth10a(true); (for my OAuthProvider)

The explanation was found in source code: // 1.0a expects the callback to be sent while getting the request token. // 1.0 service providers would simply ignore this parameter.

OTHER TIPS

In the last month, has been a change to the Twitter API. You can now only call it using HTTPS.

You should ensure that the URL you / your library is using starts with

https://api.twitter.com/1.1/

(Notice the extra s after the http.)

You may need to check with the maintainer of twitter4j.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top