I am using twitter4j-3.0.5 on Windows.
When new status arrives to the StatusListener.onStatus() method, I would like to print out it's URL but I don't find any appropriate method. status.getURLEntities() is always empty, even tough if I look at the json passed to the client, the url field is not empty and contains valid URL. I just don't know how to get this value from java.
Please advise.
Navigate to the Tweet you'd like the URL of. Click the icon located within the Tweet. From the pop-up menu, select Copy link to Tweet. The URL should now be copied to your clipboard.
The easiest way is to type twitter.com/username which is what your Twitter web address is. On desktop, login to your Twitter account in any web browser and go to your profile page to copy your Twitter URL.
The getURLEntities()
method returns any URLs in the content of the tweet itself, not the URL of the tweet.
Fortunately the URL of a tweet is in the following format:
https://twitter.com/[screen name of user]/status/[id of status]
So you can get the URL of a status with this code:
Status status;
String url= "https://twitter.com/" + status.getUser().getScreenName()
+ "/status/" + status.getId();
System.out.println(url);
URL can be exposed by 2 ways.
giving the twitter link
giving the website link which is tweeted.
package com.twitter.example;
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.URLEntity;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterApplication {
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cf = new ConfigurationBuilder();
cf.setOAuthConsumerKey("OAUTH_CONSUMER_KEY")
.setOAuthConsumerSecret("OAUTH_CONSUMER_KEY_SECRET")
.setOAuthAccessToken("OAUTH_ACCESS_TOKEN")
.setOAuthAccessTokenSecret("OAUTH_ACCESS_TOKEN_SECRET");
TwitterFactory tf = new TwitterFactory(cf.build());
Twitter twitter = tf.getInstance();
// get username and status
List<Status> statusList = twitter.getHomeTimeline();
int i = 0;
for (Status status : statusList) {
i++;
String url = "https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId();
System.out.println("Twitter URL:"+url);
URLEntity[] urls = status.getURLEntities();
for(URLEntity urlEntity : urls) {
url = urlEntity.getURL();
System.out.println("Website link URL in the tweet: "+url);
}
if (i == 1)
break;
}
}
}
Twitter URL:https://twitter.com/CulturalGM/status/966308069884952577
Website link URL in the tweet: https://websitelink
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With