在 Twitter4j 中多次获得授权 URL 时出错

Error on getting authorization URL more than once in Twitter4j

我正在使用 Twitter4j 在我的 webapp 上实现授权工作流(用户访问页面,twitter 请求许可,我收到回调并生成 oauth 访问令牌)。

我的第一个问题是,如果我调用一个方法来获取 Twitter sigleton:

Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer(getClientId(), getClientSecret());

1) 由于已经定义了 OAuthConsumer,因此我会得到一个异常。而且我找不到如何询问单例是否已经定义了凭据。最好的方法是什么?我的解决方案是将单例保存在私有成员中...

2) 现在我想生成一个AuthorizationURL,所以我需要向Twitter单例询问OAuthRequestToken:

RequestToken oauthRequestToken = twitter.getOAuthRequestToken(getCallbackURL()); //FIXME

这会引发异常:

401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
message - Invalid or expired token.
code - 89


Relevant discussions can be found on the Internet at:
 http://www.google.co.jp/search?q=3cc69290 or
 http://www.google.co.jp/search?q=45a986a5
TwitterException{exceptionCode=[3cc69290-45a986a5], statusCode=401, message=Invalid or expired token., code=89, retryAfter=-1, rateLimitStatus=null, version=4.0.4}
 at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
 at twitter4j.HttpClientBase.request(HttpClientBase.java:57)
 at twitter4j.HttpClientBase.post(HttpClientBase.java:86)
 at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:115)
 at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:92)
 at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:292)
 at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:287)
 (...)

Note: the 'Relevant discussions' links are not working as expected I think...

简而言之:

1) 我如何询问单例是否已经定义了凭据以便 'setOAuthConsumer' 不会抛出错误?

2) 如何重新请求单例生成新的authorizationURL 供用户访问和授权(再次)?

也在相应的forum

发帖

1) 我如何询问单例是否已经定义了凭据以便 'setOAuthConsumer' 不会抛出错误?

有几种方法可以做到这一点。您可以在类路径中名为 twitter4j.properties 的属性文件中设置 oAuth 消费者密钥和机密。当您使用 TwitterFactory 时,这是默认属性的来源。

如果您想以编程方式设置值,TwitterFactory 也有一些允许这样做的重载构造函数:

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CONSUMER_KEY);
    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    Twitter twitter = factory.getInstance();

2) 如何重新请求单例生成新的授权URL 供用户访问和授权(再次)?

我假设你的要求是每次都让用户授权。如果是这种情况,将通过 Twitter API 处理。有 2 个 oAuth 端点 https://api.twitter.com/oauth/authenticatehttps://api.twitter.com/oauth/authorizeauthenticate 端点是正常的 Sign in with Twitter 功能,用户将在其中批准一次,然后每次都自​​动登录。 authorize 端点每次都需要授权。

使用 Twitter4j,这些是可以在您的 RequestToken 上调用的独立方法。您根据您的要求重定向到适当的 URL。

这里介绍了我找到的解决方案:

Twitter instance = new TwitterFactory().getInstance();
instance.setOAuthConsumer(getClientId(), getClientSecret());
RequestToken requestToken = new RequestToken(getOauthToken(),getOauthTokenSecret());
AccessToken oAuthAccessToken = instance.getOAuthAccessToken(requestToken, oauthVerifier);

requestTokenoauthVerifier作为回调中的参数接收。 getOauthToken()getOauthTokenSecret() 检索库在第一步中检索到并保存在缓存中的令牌(用户 -> 令牌)。

受此启发 question/answers:Having multiple Twitter instances with twitter4j library.