开发者

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

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/status开发者_如何学Goes/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?


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜