Error processing your OAuth request: Invalid oauth_verifier parameter

∥☆過路亽.° 提交于 2019-12-06 06:34:40

You need to set oauth_verifier in the header when you are requesting https://api.twitter.com/oauth/access_token. These oauth_verifier you are getting as get parameters. So just set oauth_verifier=params[:oauth_verifier] in header.

I had the same issue and fixed it by passing the oauth_verifier in the twitter.getOAuthAccessToken() when requesting for the AccessToken.

I also had this problem with twitter4j.

I had been following the this tutorial (full code here).

However, the code wasn't brilliantly written, so I made a few changes and ended up breaking things which resulted in the "Error processing your OAuth request: Invalid oauth_verifier parameter" error.

Basically, the problem/solution lay with how I got the oauth request token from twitter4j.

Paying attention to the getRequestToken() method, this is the right way to implement it:

import android.util.Log;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

public final class TwitterUtil {

    private final static String TAG = TwitterUtil.class.getSimpleName();

    static TwitterUtil instance = new TwitterUtil();

    public static TwitterUtil getInstance() {
        return instance;
    }

    private TwitterFactory twitterFactory;
    private Twitter twitter;
    private RequestToken requestToken = null;

    private TwitterUtil() {

        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
        configurationBuilder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
        Configuration configuration = configurationBuilder.build();
        twitterFactory = new TwitterFactory(configuration);
        twitter = twitterFactory.getInstance();
    }

    TwitterFactory getTwitterFactory() {

        return twitterFactory;
    }

    void setTwitterFactory(AccessToken accessToken) {

        twitter = twitterFactory.getInstance(accessToken);
    }

    public Twitter getTwitter() {

        return twitter;
    }

    RequestToken getRequestToken() {

        if (requestToken == null) {

            try {
                requestToken = twitterFactory.getInstance().getOAuthRequestToken(Constants.TWITTER_CALLBACK_URL);
            }
            catch (TwitterException e) {
                Log.e(TAG, "Could not get Twitter OAuth Request Token", e);
            }
        }
        return requestToken;
    }

    public void reset() {

        instance = new TwitterUtil();
    }
}

And this is the wrong way to implement it:

public final class TwitterUtil_WRONG {

    private final static String TAG = TwitterUtil_WRONG.class.getSimpleName();

    static TwitterUtil_WRONG instance = new TwitterUtil_WRONG();

    public static TwitterUtil_WRONG getInstance() {
        return instance;
    }

    private TwitterFactory twitterFactory;
    private Twitter twitter;

    private TwitterUtil_WRONG() {
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
        configurationBuilder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
        Configuration configuration = configurationBuilder.build();
        twitterFactory = new TwitterFactory(configuration);
        twitter = twitterFactory.getInstance();
    }

    TwitterFactory getTwitterFactory() {
        return twitterFactory;
    }

    void setTwitterFactory(AccessToken accessToken) {
        twitter = twitterFactory.getInstance(accessToken);
    }

    public Twitter getTwitter()
    {
        return twitter;
    }

    RequestToken getRequestToken() {

        try {
            return twitterFactory.getInstance().getOAuthRequestToken(Constants.TWITTER_CALLBACK_URL);
        }
        catch (Exception e) {
            Log.e(TAG, "Could not get Twitter OAuth Request Token", e);
            return null;
        }
    }

    public void reset() {
        instance = new TwitterUtil_WRONG();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!