Cannot get the URL mentioned in the tweet

twitter4j
urls

#1

I try to use twitter4j’s getURLEntities() method to get the url in the tweets. But instead of that, status contains the shortened URL of the tweet itself not the url mentioned in the tweet.

This the URL entity I got from a tweet for example:
URLEntityJSONImpl{url=‘https://t.co/hAhMqIywwI’, expandedURL=‘https://twitter.com/i/web/status/785127700763512832’, displayURL=‘twitter.com/i/web/status/7…’}

The t.co link refers to the tweet itself but I want to get the link mentioned in the tweet.

What should I do?

Thanks.


#2

Sounds like a similar issue to what I described in my post. Your tweet is truncated which is why the URL points back to the tweet itself though I don’t have a solution for that.


#3

I got an answer on my post which solved it for me which I believe will solve it for you. Because it seems that Twitter returns a URL that points back to a post when the post/tweet is truncated, you need to get the un-truncated version.

To do this, you need to use a parameter called “tweet_mode”, see the upcoming changes to tweets documentation.

Basically, you need to supply “?tweet_mode=extended” to whatever API call you are using to get your tweet. When you do, you will receive the full tweet without truncation which in turn makes the URL in the tweet be the one you want instead of just pointing back to the tweet.


#4

Thanks for the help @MrTurnerj
Turns out that the tweets are truncated. But I could not use tweet_mode=extended parameter.
I couldn’t find where I can use it while using twitter4j for java.


#5

Yeah, had a look myself, it is unfortunate the current master of twitter4j doesn’t. I found a PR on Github about it though, it seems that others are having issues with that library as well.

I guess maybe subscribe to notifications on that PR so you know once it is merged into the master. :slight_smile:


#6

You may need to fork twitter4j yourself for the time being


#7

Thank you @MrTurnerj. I will definitely subscribe to that PR’s notifications. So, you not only showed the issue but directed me to the ongoing work about it, appreciated :slight_smile:


#8

Ok @richardhyland. I did not do forking on github before but it is simply copying this branch of development to my workstation, compiling and replacing with the existing twitter4j library, isn’t it?