geduldig/TwitterAPI

Request method fails to attach additional metadata

ERosendo opened this issue · 2 comments

I'm trying to add alt text to an image using the media/metadata/create endpoint but I'm getting the following error:

{"request":"\/1.1\/media\/metadata\/create.json","error":"Invalid json payload."}

Here's my code:

API = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)

r = API.request('media/metadata/create', params={
        'media_id': media_id,
        "alt_text": {
            "text": "dancing cat" 
        }
    })

I checked the docs for this endpoint and it says:

Requests should be HTTP POST with a JSON content body, and Content-Type application/json; charset=UTF-8 or text/plain; charset=UTF-8.

I also checked the code for the request method of the TwitterAPI class and it seems like its always using the data attribute instead of the json attribute when it tries to use v1.1 endpoints.

if method == 'POST':
if self.version == '1.1':
d = params
else:
j = params

I think We can fix this issue if We tweak the if statement. We can do something like this:

if self.version == '1.1' and 'metadata' not in resource:
    d = params
else:
    j = params

That makes sense. Do you want to submit a pull request?

Sure!