1
votes

i get a error message when i try to update my status from my twitter client. it works fine when iam reading statuses with the getHomeTimeLine() Method. i really dont know where the error could be. please help!

Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        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();
          try{
             if(pin.length() > 0){
               accessToken = twitter.getOAuthAccessToken(requestToken, pin);
             }else{
               accessToken = twitter.getOAuthAccessToken();
             }
          } catch (TwitterException te) {
            if(401 == te.getStatusCode()){
              System.out.println("Unable to get the access token.");
            }else{
              te.printStackTrace();
            }
          }
          System.out.println("token = "+accessToken.getToken());
          System.out.println("tokenSecret = "+accessToken.getTokenSecret());
        }

this is my method for the login

public void login()throws TwitterException{
    TwitterFactory factory = new TwitterFactory();
    AccessToken accessToken = new AccessToken(token, tokenSecret);
    Configuration conf = new PropertyConfiguration(new Properties());
    OAuthAuthorization auth = new OAuthAuthorization(conf);
    auth.setOAuthConsumer(consumerKey, consumerSecret);
    auth.setOAuthAccessToken(accessToken);
    twitterClient = factory.getInstance(auth);
}

this is my method for the status update

public void setStatus(String text) throws TwitterException {

    if(twitterClient==null)throw new IllegalStateException("Please login first : ");
    Status status; 
    try {
        status = twitterClient.updateStatus(text);
    } catch (TwitterException e) {
        e.printStackTrace();
        throw e;
    }
    System.out.println("Status : "+status.getText());
}

these are my error messages:

401:Authentication credentials (https://dev.twitter.com/docs/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 - Read-only application cannot POST request - /1/statuses/update.json Relevant discussions can be on the Internet at: http://www.google.co.jp/search?q=b2b52c28 or http://www.google.co.jp/search?q=1b433fc8 TwitterException{exceptionCode=[b2b52c28-1b433fc8], statusCode=401, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.6-SNAPSHOT(build: 404bccc2082954dc0d12612322791a37f3daa917)} at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:185) at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65) at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102) at twitter4j.TwitterImpl.post(TwitterImpl.java:1927) at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:433) at twitter.Zwietschi.setStatus(Zwietschi.java:74) at twitter.Zwietschi.main(Zwietschi.java:93)

where is the problem? please help!

4

4 Answers

3
votes

Check if you've changed permissions... You'll have to regenerate tokens then!!!

1
votes

This is working for me, you should try with the ConfigurationBuilder.

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("YOUR_APP_KEY");
cb.setOAuthConsumerSecret("YOUR_APP_SECRET");
cb.setOAuthAccessToken("USER_TOKEN_KEY");
cb.setOAuthAccessTokenSecret("USER_TOKEN_SECRET");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();   
String tweet="HELLO WORLD!";
twitter.updateStatus(tweet);
1
votes

Seems like as of today (jan 14) this error is also thrown, when not using an ssl connection.

See REST API, regarding 403 error code? for solution (enable SSL, use latest twitter4j).

1
votes

This implementation worked for me in version Twitter4J API 3.0.3.

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(Config.CONSUMER_KEY)
.setOAuthConsumerSecret(Config.CONSUMER_SECRET)
.setOAuthAccessToken(Config.ACCESS_TOKEN)
.setOAuthAccessTokenSecret(Config.ACCESS_TOKEN_SECRET);
cb.setUseSSL(true);                 
TwitterFactory twitterFactory = new TwitterFactory(cb.build());
twitter = twitterFactory.getInstance();

The line that makes it works is

cb.setUseSSL(true); 

Heard that is the default on Twitter4J API 3.0.5.