简体   繁体   English

Twitter4J验证凭据并捕获错误

[英]Twitter4J verify Credentials and catching error

I have the following code on google appengine to test if twitter credentials are valid using twitter4j 我在谷歌appengine上有以下代码来测试twitter凭据是否有效使用twitter4j

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    String Test = "Testing Twitter4J";
    try {

        ConfigurationBuilder confbuilder  = new ConfigurationBuilder();
        confbuilder.setOAuthAccessToken("A") 
        .setOAuthAccessTokenSecret("B") 
        .setOAuthConsumerKey("C") 
        .setOAuthConsumerSecret("D"); 
        Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance(); 

        //Status status = twitter.updateStatus("Working lunch today");
        User user = twitter.verifyCredentials();
        Test = "Successfully updated the status to [" + user.getScreenName() + "].";

    } catch (TwitterException e) {
        Test =  "no";
    }
    try {
        resp.getOutputStream().write(Test.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return;
}

If the credentials are valid it works as expected. 如果凭据有效,则按预期工作。 If I revoke access so the credentials are not valid I get an app engine error: 如果我撤销访问权限以使凭据无效,则会收到应用引擎错误:

Uncaught exception from servlet
twitter4j.TwitterRuntimeException: 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=93f2523c or
    http://www.google.co.jp/search?q=8b74a4e3
TwitterException{exceptionCode=[93f2523c-8b74a4e3], statusCode=401, message=Invalid or expired token, code=89, retryAfter=-1, rateLimitStatus=null, version=3.0.3}

from the "User user = twitter.verifyCredentials();" 来自“User user = twitter.verifyCredentials();” line. 线。 I was expecting the TwitterException to be thrown from the javadoc at: http://twitter4j.org/javadoc/twitter4j/api/UsersResources.html#verifyCredentials() but it seems I get a TwitterRuntimeException. 我期待TwitterException从javadoc抛出: http//twitter4j.org/javadoc/twitter4j/api/UsersResources.html#verifyCredentials()但似乎我得到了一个TwitterRuntimeException。 Why? 为什么? I can't find any notes of a TwitterRuntimeException in the javadocs. 我在javadocs中找不到任何关于TwitterRuntimeException的注释。

For me the solution was the following: 对我来说,解决方案如下:

  • Go to your twitter app 转到您的Twitter应用程序
  • Select the settings tab 选择设置选项卡
  • Under Application Type - Access - Select the last option Read,Write and Access direct messages 在应用程序类型 - 访问 - 选择最后一个选项读取,写入和访问直接消息

If this doesn't solve the problem try adding Callback URL. 如果这不能解决问题,请尝试添加回调URL。 You can put any url here it will be fine. 你可以在这里放任何网址就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM