401 On twitter.media.mediaUpload()
vsnthdev opened this issue ยท 4 comments
Describe the bug
Below are the authentication requirements by Twitter for twitter.tweets.statusesUpdate()
and twitter.media.mediaUpload()
endpoints respectively ๐
and
As they require same level of authentication if twitter.tweets.statusesUpdate()
works then twitter.media.mediaUpload()
should also work. Where twitter.tweets.statusesUpdate()
works as expected, twitter.media.mediaUpload()
responds with a 401
. Here's the code I've used ๐
const res = await twitter.media.mediaUpload({
media: await fs.readFile(image.path)
})
console.log('res ', res)
And here's the response I get ๐
{
statusCode: 401,
data: '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'
}
To reproduce
Steps to reproduce the behavior:
- Create a new Node.js project and add
twitter-api-client
as dependency. - Write the following code ๐ in a file named
index.js
const fs = require('fs/promises')
const res = await twitter.media.mediaUpload({
media: await fs.readFile('./image.png')
})
console.log('res ', res)
- Add a dummy image in the same folder as
index.js
- Run it!
Expected behavior
I see the newly uploaded image in Twitter Media Studio.
Package Manager:
To install the Twitter API Client, I used Yarn v1.22.10
Additional context
Node.js: v15.4.0
Module System: Native ESM
Operating System: macOS Big Sur 11.1 (20C69)
Hi @vasanthdeveloper ๐
Thanks for creating this issue.
The mediaUpload
endpoint expects the media
to be base64
encoded.
So - without having tried your reproduction example yet - could you first try to do the following:
const fs = require('fs/promises')
const res = await twitter.media.mediaUpload({
media: await fs.readFile('./image.png', { encoding: 'base64' }) // <- add base64 here
})
console.log('res ', res)
I have a feeling that will work.
Thank you.
@SimonHoiberg Yup! that works ๐ฅณ you're a genius! ๐
@SimonHoiberg It would be great if you make an example of chunked upload since that's tricky ๐