daneden / Twift

🐦 An async Swift library for the Twitter v2 API 🚧 WIP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add/fix media methods

daneden opened this issue · comments

The biggest blocker to general release of Twift is media upload methods. Unfortunately, they're proving extremely difficult to get working.

I’m using the same method to OAuth sign requests as all the other API calls, with the exception that I’m ignoring query and form parameters since the documentation says that’s how media upload POST calls should be signed:

Because the method uses multipart POST, OAuth is handled differently. POST or query string parameters are not used when calculating an OAuth signature basestring or signature. Only the oauth_* parameters are used.

Despite all other API calls working with the signing method, and despite following the above instructions, all my attempts to call the /1.1/media/upload.json?command=INIT&media_type=image/jpeg&total_bytes=123456 endpoint result in the same unhelpful error:

{
  "errors": [
    {
      "code": 32,
      "message": "Could not authenticate you."
    }
  ]
}

I’ve tried everything I can think of:

  • URL percent encoding the query parameters
  • Passing the parameters as the request's HTTP body
  • Doing the above and URL percent encoding them
  • Ignoring the documentation's advice and passing the parameters to the OAuth signing method
  • Deliberately malforming the request method (e.g. request.method = "POOP") to try and elicit a different response

This is my white whale.

lmao ok this was ridiculous; basically the fix was to ignore(?) Twitter's own advice in the Developer Documentation and still encode the form parameters in the POST request OAuth signature.

Now that this method is working, I need to:

  • Test it with different media types
  • Add methods for periodically checking the processing status
  • Add method(s) for appending metadata to the media (e.g. alt text)