Giter Site home page Giter Site logo

Comments (4)

NiciusB avatar NiciusB commented on August 21, 2024

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))
    })
}

from twitter-lite.

dylanirlbeck avatar dylanirlbeck commented on August 21, 2024

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

from twitter-lite.

NiciusB avatar NiciusB commented on August 21, 2024

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

from twitter-lite.

peterpme avatar peterpme commented on August 21, 2024

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

from twitter-lite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.