使用java Oauth2的无效或过期令牌在推文应用程序

时间:2016-11-25 10:33:22

标签: java twitter oauth-2.0

我尝试使用以下代码但由于某种原因我收到了无效或过期的令牌 代码下方

 public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cf= new ConfigurationBuilder();
cf.setDebugEnabled(true)
        .setOAuthConsumerKey("3Tt35OwMYXXXXXXIsLqVDf0Y")
        .setOAuthConsumerSecret("eMTbKXWvCgrc4k3XXXXXXXXXXXXXXXhJ1MWEtEFZnRZ")
        .setOAuthAccessToken("SonrEAXXXXXXXXXdD4UzcmBUuP4vsVmwZ4ZhHQF5")
        .setOAuthAccessTokenSecret("e1wR16ralA2ht0XXXXXXXXXXNhN5iv9izk1K1magOx");

    TwitterFactory tf =new TwitterFactory(cf.build());
    twitter4j.Twitter twitter= tf.getInstance();
    List<Status> status=twitter.getHomeTimeline();

    for(Status st:status){
        System.out.println(st.getUser().getName()+""+st.getText());
    }
}

我在运行课程时遇到错误

 Exception in thread "main" 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=62590fa6 or http://www.google.co.jp/search?q=08f4a3c5
TwitterException{exceptionCode=[62590fa6-08f4a3c5], 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.get(HttpClientBase.java:75)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1786)
at twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:105)
at firsttwitapp.FirstTwitApp.main(FirstTwitApp.java:34)

C:\ Users \ User1 \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ debug.xml:83:Java返回:1 BUILD FAILED(总时间:1分23秒)

1 个答案:

答案 0 :(得分:2)

当我尝试制作TwitterBot时,我遇到了同样的问题。此问题是由于系统的代理问题,默认情况下为false。你可以用这个简单的一行代码打开它:

System.setProperty("java.net.useSystemProxies", "true");