I am trying to get the media_id for a media upload. See docs here.

When using postman, my request is processed successfully and I get a response like this:

{
    "media_id": 1222234872222222401,
    "media_id_string": "1222734822222102201",
    "expires_after_secs": 86399
}

Unfortunately, using postman for our app is not an option. However, when I post a tweet with just text, the tweet is posted successfully using our own native code. I have also recreated the request from postman, and can successfully recreated the same oauth_signature needed for the media upload authorization. So I know that the backend is working, but I think I need some help structuring the POST request itself.

Here is the code (Lucee ColdFusion):

mediaEndpoint = "https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=10240&media_type=image/jpg&oauth_consumer_key=consumerKeyHere&oauth_token=tokenHere&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1653075352&oauth_nonce=UU5V18WLaPN&oauth_version=1.0&oauth_signature=verifiedSignature";
cfhttp(url=mediaEndpoint, method="POST", result="init") {
        cfhttpparam(type="header", name="Content-Type", value="application/x-www-form-urlencoded");
        cfhttpparam(type="header", name="Accepts", value="*/*");
        cfhttpparam(type="header", name="Accept-Encoding", value="gzip, deflate, br");
        cfhttpparam(type="header", name="Connection", value="keep-alive");
        cfhttpparam(type="body", value="command=INIT&media_type=#mediaParameters.media_type#&total_bytes=#mediaParameters.total_bytes#");
    }

But I keep getting the following 401:
{"errors":[{"code":32,"message":"Could not authenticate you."}]}

I think this is a case of wrong oAuth - your parameters aren’t sorted alphabetically maybe? or something else wrong in the signature? Creating a signature | Docs | Twitter Developer Platform

You might not have read the description. As mentioned above, I have been able to recreate the same signature used in the postman response that did not return an error. Alphabetizing the parameters is required for generating a valid signature.

The issue (I am assuming) must lie in how my request is being structured. As you suggested I altered the endpoint to contain all of the parameters in alphabetical order, but this still returned an error. The string shown above is identical to the one from postman.

1 Like

I thought it would probably be helpful to see the postman requests I keep referring to, so here are some screen shots:


The generated curl:

curl --location --request POST 'https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=10240&media_type=image/jpg' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: guest_id=v1%11111111111111111111; guest_id_ads=v1%11111111111111111111; guest_id_marketing=v1%11111111111111111111; personalization_id="v1_1111111111111111111111=="; lang=en' \
--data-urlencode 'oauth_consumer_key=key here' \
--data-urlencode 'oauth_token=token here' \
--data-urlencode 'oauth_signature_method=HMAC-SHA1' \
--data-urlencode 'oauth_timestamp=1653137815' \
--data-urlencode 'oauth_nonce=tKA8TUs4GDk' \
--data-urlencode 'oauth_version=1.0' \
--data-urlencode 'oauth_signature=signature here'

1 Like