简体   繁体   中英

How to get URL preview of link shared in tweet using Twitter API?

I'm using the Tweet Lookup API (part of V2). When a user shares a link in a tweet, a preview of that URL is generated in that tweet. I want to recreate this using API. So how can I get the preview image of the URL and also other details like the domain, etc?

For example, if you look at this tweet: https://twitter.com/elonmusk/status/1429907171639103489 在此处输入图像描述

If you see the above tweet, there's a URL preview card, with image of starlink and a brief description of the link. How to get these details through the Twitter API?

I suppose I am a little late, but..!

Unfortunately, to my knowledge there is no way to get the preview image from the twitter API.

You have to find the link from the tweet, get that site and scrape the image from there. Websites can tell twitter what image they want to use by making a tag that has the property "twitter:image"!

--

What you can do, for example, is get the original URL for the post from entities!

Eg making the tweet look-up like this: https://api.twitter.com/2/tweets/1532014165686206466?tweet.fields=entities

From there, get the original url, for example from the "unwound_url" in the JSON response of that look-up example.

You need to make a request to that site and from the html response, look for a tag that has the property set to "twitter:image"

In that tags content you have your image link!

See:

https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets-id

https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/tweet

I hope my rambling made sense and good luck!

Unfortunately, this is impossible because Twitter requires URLs with Twitter meta tags in order to show these Links as cards with images. So the only way to do that is to add these meta Tags in the head of your website:

 <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:title" content="LINK TITLE HERE"> <meta name="twitter:description" content="LINK DESCRIPTION HERE"> <meta name="twitter:image" content="PREVIEW IMAGE HERE">

This is how I made it on my own website. I hope that will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM