geduldig / TwitterAPI

Minimal python wrapper for Twitter's REST and Streaming APIs
936 stars 263 forks source link

Request method fails to attach additional metadata #224

Closed ERosendo closed 1 year ago

ERosendo commented 1 year ago

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.

https://github.com/geduldig/TwitterAPI/blob/5c07c02fce51302e747190a47b6c6796fee23352/TwitterAPI/TwitterAPI.py#L203-L207

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
geduldig commented 1 year ago

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

ERosendo commented 1 year ago

Sure!