twitter4j: getting credential errors even though i had set them?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:49:58

问题


package twitter4j.examples.tweets;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.*;

import java.io.IOException;

public final class UpdateStatus {

    public static void main(String[] args) throws IOException {

        String testPost = "hello from otc";
        String consumerKey = "key";
        String consumerSecret = "secret";
        String accessToken = "access";
        String accessSecret = "access_secret";

        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);

        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter twitter = factory.getInstance();
            AccessToken accestoken = new AccessToken(accessToken, accessSecret);

            twitter.setOAuthAccessToken(accestoken);
            Status status = twitter.updateStatus(testPost);
            System.out.println("it worked!");
            if (status.getId() == 0) {
                System.out
                        .println("Error occured while posting tweets to twitter");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

why do i keep getting this error:

401:Authentication credentials (http://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync. error - Could not authenticate with OAuth. request - /1/statuses/update.json Relevant discussions can be on the Internet at: http://www.google.co.jp/search?q=e06d87a8 or http://www.google.co.jp/search?q=5851cbdb TwitterException{exceptionCode=[e06d87a8-5851cbdb], statusCode=401, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.3} at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189) at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65) at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102) at twitter4j.TwitterImpl.post(TwitterImpl.java:1871) at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:459) at twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:35)

i have set the credentials in the file already, have i not?


回答1:


i figured it out heres a working code:

package twitter4j.examples.tweets;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

import java.io.IOException;

public final class UpdateStatus {

    public static void main(String[] args) throws IOException {

        String tweet = "your first tweet via java";

        String accessToken = "your access token";
        String accessSecret = "your access token secret";

        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter twitter = factory.getInstance();
            AccessToken accestoken = new AccessToken(accessToken, accessSecret);

            twitter.setOAuthAccessToken(accestoken);
            Status status = twitter.updateStatus(tweet);
            System.out.println("it worked!");
            if (status.getId() == 0) {
                System.out
                        .println("Error occured while posting tweets to twitter");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

note: for this to work you MUST create an application on twitter you must have a twitter4j.properties file which contain the following

debug set to true
oauth.consumerKey 
oauth.consumerSecret 
oauth.accessToken 
oauth.accessTokenSecret

all the keys and tokens are from the twitter application make sure your application access level all say "read and write"



来源:https://stackoverflow.com/questions/6681399/twitter4j-getting-credential-errors-even-though-i-had-set-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!