Cannot use an uploaded image to create a tweet: INVALID_MEDIA returned

image
media-upload
api

#1

Hello everyone,

We are experiencing an issue with the media_id and media_id_string returned by the https://upload.twitter.com/1.1/media/upload.json endpoint.

We are sending a request containing the parameter media_data with a base64 encoded value, and we got an invalid id right after.

Please see the example call below (I reduced the base64 but it’s a valid image that fits with Twitter requirements).

Input:

{
    "url": "https:\/\/upload.twitter.com\/1.1\/media\/upload.json",
    "method": "POST",
    "parameters": {
        "media_data": "\/9j\/4AAQSkZJRgABAQAAAQABAAD\/4QENRXhpZgAATU0AKgAAAAg[...]"
    }
}

Output:

{
    "media_id": 845257652766720004,
    "media_id_string": "845257652766720004",
    "size": 72716,
    "expires_after_secs": 86400,
    "image": {
        "image_type": "image\/jpeg",
        "w": 600,
        "h": 338
    }
}

Then, we try to use the uploaded image to create a tweet and it’s not working.

Input:

{
    "url": "https:\/\/ads-api.twitter.com\/1\/accounts\/18ce54h5wcy\/tweet\/",
    "method": "POST",
    "parameters": {
        "status": "Test do not use",
        "as_user_id": "798425578177171456",
        "media_ids": 845257652766720004
    }
}

Output:

{
    "errors": [
        {
            "code": "INVALID_MEDIA",
            "message": "Invalid media ids"
        }
    ],
    "request": {
        "params": {
            "status": "Test do not use",
            "media_ids": [
                845257652766720004
            ],
            "as_user_id": 798425578177171456,
            "account_id": "18ce54h5wcy"
        }
    }
}

Can you please help us to understand why it’s happening?

Best,
Christopher Chauve.


#3

Hey @c_chauve

This could potentially be a permissions issue with the media file itself. As per our documentation, there is an additional_owners param that can be passed when uploading a media. It looks like during the tweet creation step, you’ve got the following param value as_user_id:798425578177171456.

Can you confirm that this user_id is passed in as an additional_owner when uploading the image?

Thanks!


#5

Hello @imit8me,

Thanks for your help. We managed to fix the issue.

Best,
Christopher


#6

Hey @c_chauve

Great to hear! Can you let us know how you were able to fix the issue and consider marking this thread as resolved?

Thanks!


#7

Hi @imit8me,

Since it was not a permission issue, we checked again.
The error was saying that the media id was invalid, we changed the way we sent it.

Before, we were using the id so we tried using the id_str of the media and it was working as expected. For some reasons, id and id_str are not always the same id.

It’s pretty confusing, but now we managed to create the tweet as expected.

Best,
Chris


#8

This is most likely caused by the language you are using not coping well with large integers (this commonly happens with Javascript) - you can read more about it in our documentation. Always use id_str by preference.