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

Endpoint "media/metadata/create" fails #67

Closed
NiciusB opened this issue Mar 11, 2020 · 4 comments · Fixed by #81
Closed

Endpoint "media/metadata/create" fails #67

NiciusB opened this issue Mar 11, 2020 · 4 comments · Fixed by #81

Comments

@NiciusB
Copy link
Contributor

NiciusB commented Mar 11, 2020

The endpoint https://upload.twitter.com/1.1/media/metadata/create.json fails due to content being sent in application/x-www-form-urlencoded, but it only accepts application/json
I tried adding it to JSON_ENDPOINTS but it fails with an invalid json response.

Code sample:

const twUploadClient = new Twitter({
  subdomain: 'upload',
  consumer_key: credentialsArray[0],
  consumer_secret: credentialsArray[1],
  access_token_key: credentialsArray[2],
  access_token_secret: credentialsArray[3]
})

// Upload image
const mediaUploadData = await twUploadClient.post('media/upload', { media_data: b64content })

// Set alt text for image
if (card.alt && card.alt.length > 0) {
  await twUploadClient.post('media/metadata/create', { media_id: mediaUploadData.media_id_string, alt_text: { text: imageAltString } })
}

Doc page for media/metadata/create for reference: https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-metadata-create

@NiciusB
Copy link
Contributor Author

NiciusB commented Mar 11, 2020

It seems like it actually makes a successful request, but it fails to parse the result.

Here is some extremely hacky code if anyone needs it. It's bad, but it will not throw

const crossFetch = require('cross-fetch')

function customTwitterPostForAltText (twitterClient, body) {
  const resource = 'media/metadata/create'
  const baseHeaders = {
    'Content-Type': 'application/json',
    Accept: 'application/json'
  }

  const { requestData, headers } = twitterClient._makeRequest(
    'POST',
    resource,
    null // don't sign JSON bodies; only parameters
  )

  const postHeaders = Object.assign({}, baseHeaders, headers)
  body = JSON.stringify(body)

  return crossFetch(requestData.url, {
    method: 'POST',
    headers: postHeaders,
    body
  })
    .then(res => {
      if (res.status !== 200) throw new Error('[customTwitterPostForAltText] ' + JSON.stringify(res))
    })
}

@dylanirlbeck
Copy link
Contributor

Hi @NiciusB, sorry for the delay. I'm a bit confused, did you solve this issue with the snippet you posted above?

@NiciusB
Copy link
Contributor Author

NiciusB commented Mar 24, 2020

I did, but it's not ideal since it's ignoring the result of the request. If the request succeeds we have no access to the response, and if twitter returns an error, the actual error message will be lost.

Basically what I wanted to do was to upload an image with a caption.

// Upload image
const b64content = '' // Base64-encoded image
const mediaUploadData = await twUploadClient.post('media/upload', { media_data: b64content })

// Set alt text for image
const altTextString = 'Animated image of a cat failing a jump'
await customTwitterPostForAltText(twUploadClient, { media_id: mediaUploadData.media_id_string, alt_text: { text: altTextString } })

// Tweet
const tweetData = await twClient.post('statuses/update', {
  status: 'Check out my accesible image',
  media_ids: [mediaUploadData.media_id_string]
})

For reference this is the original file: https://github.com/NiciusB/valentine-cards-bot/blob/master/src/cron/sendTweet.js

@peterpme
Copy link
Contributor

Hey @NiciusB do you mind creating a PR that would fix this for our users? We'd really appreciate it, thanks!

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

Successfully merging a pull request may close this issue.

3 participants