Java Twitter4j OAuth无法正常工作

时间:2016-07-16 21:10:33

标签: java oauth twitter4j

我正在阅读this question Twitter4j的例子。我试图获得#7:OAuth支持工作,但我遇到了麻烦。我有以下代码:

try {
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer("VALID KEY FROM APP HERE", "VALID SECRET FROM APP HERE");
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while(null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        if(pin.length() > 0){
            accessToken = twitter.getOAuthAccessToken(requestToken, pin);
        } else {
            accessToken = twitter.getOAuthAccessToken();
        }
    }
    System.out.println(twitter.verifyCredentials().getId());
    System.out.println(accessToken);
} catch(Exception e) {
    e.printStackTrace();
}

我的控制台中的输出如下:

Open the following URL and grant access to your account:
https://api.twitter.com/oauth/authorize?oauth_token=<REMOVED>
Enter the PIN(if aviailable) or just hit enter.[PIN]:

当我复制原始链接并转到我的网络浏览器中的URL时,它会打开预期的Twitter应用程序授权页面。当我授予它访问权限时,它会按预期将我发送到应用程序的回调(不存在)。我的控制台中没有给出一个引脚,所以我只需按回车键,结果就是:

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.
Error processing your OAuth request: Invalid oauth_verifier parameter

Relevant discussions can be found on the Internet at:
        http://www.google.co.jp/search?q=17bf128c or
        http://www.google.co.jp/search?q=12b8f614
TwitterException{exceptionCode=[17bf128c-12b8f614], statusCode=401, message=null
, code=-1, 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.getOAuthAccessToken(OAuthAuthorizat
ion.java:125)
        at twitter4j.TwitterBaseImpl.getOAuthAccessToken(TwitterBaseImpl.java:33
6)
        at tweetserver.TweetServer.main(TweetServer.java:51)

我已经多次检查了我的应用程序的密钥和密码,并将它们复制/粘贴到我的代码中。我还在PHP应用程序中使用了相同的密钥/密钥。在Twitter4j中使用访问令牌和访问令牌机密(允许直接访问我的帐户使用API​​而无需任何权限),没有任何问题。

我相信我不明白这个例子,我怎么能解决这个问题呢?谢谢你的阅读。

1 个答案:

答案 0 :(得分:0)

我假设您已使用自己的凭据自定义此行?

twitter.setOAuthConsumer("VALID KEY FROM APP HERE", "VALID SECRET FROM APP HERE");