处理您的OAuth请求时出错:oauth_verifier参数无效

时间:2015-07-29 11:39:24

标签: twitter oauth twitter4j

我跟随https://dev.twitter.com/web/sign-in/implementing在我的应用程序中使用twitter实现OAuth注册。

以下是向Twitter提供授权此应用程序对话框的服务:来自twitter:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/twitter")
public Response redirectToTwitter() {
    Configuration cfg = new ConfigurationBuilder().setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
                                                  .setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
                                                  .build();
    Twitter twitter = new TwitterFactory(cfg).getInstance();
    String callbackURL = "https://localhost:9090/app/ui/oauth/twitter";
    try {
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
        String authURL = requestToken.getAuthenticationURL();
        return Response.seeOther(URI.create(authURL)).build();
    } catch (TwitterException e) {
        LOG.error(e.getMessage(), e);
        return ErrorResponse.create(e.getMessage());
    }
}

这样可以将浏览器重定向到要求授权的Twitter页面。当我单击“登录”时,会出现重定向对话框,该对话框会将我重定向到以下URL:

https://localhost:9090/app/ui/oauth/twitter?oauth_token=6oWQxQAAAAAAgyORAAABTtmVVFM&oauth_verifier=GMX5SiqnkFfUu2MgirTDJnkJmtHZXn5H

@GET
@Path("/twitter")
public SocialSignInView login(@QueryParam("oauth_token") String token,
                              @QueryParam("oauth_verifier") String verifier) {
    Configuration cfg = new ConfigurationBuilder().setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
            .setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
            .setOAuthAccessToken(token)
            .build();
    Twitter twitter = new TwitterFactory(cfg).getInstance();
    String callbackURL = "https://localhost:9090/app/ui/oauth/twitter";
    String screenName = null;
    try {
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
        screenName = accessToken.getScreenName();
    } catch (TwitterException e) {
        LOG.error(e.getMessage(), e);
    }

    return new SocialSignInView(screenName);
}

此时我已经拥有了所有必需的参数 - 根据https://dev.twitter.com/web/sign-in/implementing 3. - 要检索访问令牌,但是,我不知道如何将现有的{{{{{{ 1}}。

使用上面的代码,我收到以下错误:

oauth_token

2 个答案:

答案 0 :(得分:0)

我有同样的问题,并通过传递oauth_verifier来修复它 请求获取AccessToken时的twitter.getOAuthAccessToken()。

答案 1 :(得分:0)

我也遇到了 twitter4j 这个问题。

我一直在关注this tutorialfull code here)。

但是,代码编写得不够出色,因此我进行了一些更改并最终破坏了代码,导致出现“ 处理您的OAuth请求时出错:无效的oauth_verifier参数”错误。

基本上,问题/解决方案取决于我如何从twitter4j获取oauth请求令牌。

请注意getRequestToken()方法,这是实现该方法的正确方法

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();
    }
}

这是错误的实现方式

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();
    }
}