0

I'm trying to embed tweets based on users' search terms.

I can retrieve tweets from Twitter's search API, but then none of the URLs I get back in twitter's response are compatible with the oEmbed API, so I can't display the tweets I'm getting.

I send a GET to https://api.twitter.com/1.1/search/tweets.json?q=${VARIABLE}, and here are all of the URLs I get back:

display_url: "twitter.com/i/web/status/1…"
expanded_url: "https://twitter.com/i/web/status/1328842765115920384"
url: [shortened URL that Stack Overflow will not let me print]

However, for the oEmbed API, the url needs to be in the format of https://twitter.com/[username]/status/[id].

Can anyone help as to how I can utilise the search api and then display those tweets on screen?

1 Answer 1

0

The important thing here is that you have retrieved truncated Tweets, and you need the full Tweet in order to have the correct URL.

First of all, add ?tweet_mode=extended to the API call. This parameter is needed in API v1.1 in order to retrieve Tweets that are longer than 140 characters.

GET https://api.twitter.com/1.1/search/tweets.json?q=${VARIABLE}&tweet_mode=extended

Once you've done that, you'll have the full Tweet object. However, it will not contain a link to itself - the only reason you had the link before is that you were retrieving the truncated Tweet, so the API returned a link to the complete Tweet.

The way to construct the original Tweet URL is to combine the Tweet ID with a Twitter status URL. It doesn't matter what username you put in, because Twitter will resolve that from the ID.

So in this case, you have Tweet ID 1328842765115920384, and you can append it to https://twitter.com/twitter/status/ to give https://twitter.com/twitter/status/1328842765115920384 which will resolve to https://twitter.com/Ashton5SOS/status/1328842765115920384 automatically.

Pass this URL to the publish.twitter.com oEmbed endpoint.

There's more information in this DEV post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.