Skip to content

Media from Tweet lookup #195

@raphaelsiz

Description

@raphaelsiz

So I'm trying to fetch Tweets and copy the text and media so that users can easily repost the Tweet (with attribution) in order to add alt text to images.
I've figured out how to get the text and other tweet attributes, including media, but the media is only showing me the media keys which are absolutely not useful to me.
Here's the curl request that was suggested by the Twitter v2 API calls tool:

curl "https://api.twitter.com/2/tweets/<ID>?tweet.fields=text,attachments&user.fields=username&media.fields=url" -H "Authorization: Bearer $BEARER_TOKEN"

So I took that and did this in the client:

const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({id})
};
let res = await fetch('/tweet',options)
let json = await res.json();

and this in the server:

app.post('/tweet',(req,res)=>{
  client.get("tweets",{ids:req.body.id, 'media.fields': 'url','tweet.fields':'attachments,text'}).then(r=>{
    res.json(r.data)
    console.log(r.data)
  }).catch(err=>{
    console.log(err)
    res.json({status: 'failure', reason: err.detail})
  })
});

Both in the server console.log and in the response json object sent back to the client, it was an object like this:

{
  attachments: {
    media_keys: [
      "3_<Tweet ID>"
    ]
  }
  id: "<Tweet ID>"
  text: "<Tweet text> <URL, presumably referring to the image, but that does not lead to anything>"
}

Is there a way to get the attachment urls? I also tried other media.fields and none of them worked. Is there no implementation of media.fields in this or am I just doing something wrong?
(note, in case it wasn't clear by there being a media key returned, there is an image in the Tweet I looked up)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions