Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media from Tweet lookup #195

Open
raphaelsiz opened this issue Apr 25, 2022 · 0 comments
Open

Media from Tweet lookup #195

raphaelsiz opened this issue Apr 25, 2022 · 0 comments

Comments

@raphaelsiz
Copy link

raphaelsiz commented Apr 25, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant