Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post a Tweet with twitter4j

I have a twiiter app with:

// Bearer token

// API key

// API key secret

// Access token:

// Access token secret:

and Read, Write, and Direct Messages permissions

I use a ConfigurationBuilder class to configure Twitter4J programmatically in Java:

ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey("IX9qalH5azhSk61ZoFMiL83Jg")   // API key
            .setOAuthConsumerSecret("tidoT2GgVZE3B3txDb64tuinlv0wZHbKesX5bNIcjPWpxmcYcs") // API key secret
            .setOAuthAccessToken("1345093646195010258-goY3W4oyqSltGFSUNPwJy8pSjSmfI5") // Access token:
            .setOAuthAccessTokenSecret("ijkpYB8sacVcBdvKHOnh95Y3QRITzD3c4Uq8nUUMLTN8O"); //Access token secret:
    TwitterFactory tf = new TwitterFactory(cb.build());
    return tf.getInstance();

But I have this error when I try to publish:

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.
{"request":"\/1.1\/media\/upload.json","error":"Read-only application cannot POST."}

I send the tweets from an Ubuntu server , already sync (https://vitux.com/how-to-sync-system-time-with-internet-time-servers-on-ubuntu-20-04/)

root@localhost:~# timedatectl
               Local time: Wed 2021-01-06 08:03:11 UTC
           Universal time: Wed 2021-01-06 08:03:11 UTC
                 RTC time: Wed 2021-01-06 08:03:12    
                Time zone: Etc/UTC (UTC, +0000)       
System clock synchronized: yes                        
              NTP service: active                     
          RTC in local TZ: no 
like image 980
Nunyet de Can Calçada Avatar asked Jan 05 '21 10:01

Nunyet de Can Calçada


People also ask

How do you post a tweet in Java?

Use Twitter REST API directly in Java invoke the URL https://api.twitter.com/1.1/statuses/update.json with required parameters. To make these REST call lets use oauth-signpost and Apache Commons HTTP. Following are the dependent jars to be used, commons-codec-1.6.

What is Twitter4J?

Twitter4J is an open source Java library, which provides a convenient API for accessing the Twitter API. Simply put, here's how we can interact with the Twitter API; we can: Post a tweet. Get timeline of a user, with a list of latest tweets. Send and receive direct messages.


Video Answer


1 Answers

From this response below, I think you are using this API.

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.
{"request":"\/1.1\/media\/upload.json","error":"Read-only application cannot POST."}

As there is an error message like below you need to create a Twitter application with Read & Write permissions. For more details about error messages, check this.

Read-only application cannot POST.

Here is the official developer document about app permissions. Change this: Read only app to this: Read and write app

I hope, this is clear enough.

like image 141
Muhammed Kılıç Avatar answered Oct 23 '22 00:10

Muhammed Kılıç