I am using twitter4j for accessing the v1 of twitter API. As some of you know, v1 is getting deleted/deprecated by 4/29/2023.
For v2 twitter API, I found I need to add the v2 twitter4j wrapper to my project to use the v2 APIs. https://github.com/takke/twitter4j-v2
I thought it'd be simply adding the depecency to my pom, but not so. It's written in Kotlin. I added the dependency and it compiles just fine, I can see the .class files of the lib, but when I tried to import, it's not found.
I do see this message when I do clean install:
[WARNING] No sources found skipping Kotlin compile
I am using intellij. Is there a setting I need to change?
This is snippet of my pom:
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>io.github.takke</groupId>
<artifactId>jp.takke.twitter4j-v2</artifactId>
<version>1.4.1</version>
</dependency>
This is probably rookie mistake I'm making since I never worked with Kotlin before.
I faced the same problem and then switched over to the new Twitter API.
<dependency>
<groupId>com.twitter</groupId>
<artifactId>twitter-api-java-sdk</artifactId>
<version>2.0.3</version>
</dependency>
Here is an example
public TwitterService(@Value(value = "${is.local.dev}") final boolean localDevelopment,
@Value(value = "${twitter.bearerToken}") final String bearerToken) {
this.localDevelopment = localDevelopment;
TwitterCredentialsBearer credentials = new TwitterCredentialsBearer(bearerToken);
this.twitterApi = new TwitterApi(credentials);
}
As the Spring bean is a singleton it should be sufficient to build it inside the constructor (happens once)
try {
TweetsApi.APIcreateTweetRequest tweet = getTwitterApi().tweets().createTweet(new TweetCreateRequest().text(what));
ApiResponse<TweetCreateResponse> response = tweet.executeWithHttpInfo();
if (response.getStatusCode() != 200) {
log.error("Could not tweet. Error code: {}, reason: {}", response.getStatusCode(), response.getData());
}
log.debug("Tweet created");
} catch (ApiException e) {
log.error("Failed to create a tweet", e);
}
Edit:
If you only want to post tweets and dont need the convenience of the Twitter SDK, there is an easier approach using OAuth1 - because the Bearer token cannot be used for all endpoints plus the OAuth2 is kind of frustrating. Especially because the Twitter V2 documentation is as bad as it could actually be. So bad explained and so weird implemented imho. SO in case you only want to tweet automatically, you can use this class to generate your Authentication:
And then just use it like this:
public void tweet(String what) {
TwitterOAuthGenerator authGenerator = new TwitterOAuthGenerator(apiKey, apiKeySecret, accessToken, accessTokenSecret);
String header = authGenerator.generateHeader("POST", TWEET_URL, Collections.emptyMap());
HttpResponse<String> response = Unirest.post(TWEET_URL)
.header("Authorization", header)
.header("Content-Type", "application/json")
.body("{\"text\":\"" + what + "\"}")
.asString();
}