I have been trying to access Twitter’s Ads API and kept getting the following error:
{"errors":[{"code":"UNAUTHORIZED_ACCESS","message":"This request is not properly authenticated"}],"request":{"params":{}}}
for URL: https://ads-api.twitter.com/0/accounts/:ACCOUNTID/campaigns?sort_by=start_time-desc
with Authorization Header:
OAuth oauth_consumer_key="<Consumer Key (API Key)>", oauth_nonce="bb829f56bec74c9fbb161a198743ccb3", oauth_signature="<generated oauth signature>", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1430413665", oauth_token="<Access Token>", oauth_version="1.0"
My app has Read-Write permission and has Consumer Key (API Key), Consumer Secret (API Secret), Access Token, Access Token Secret.
If I use the OAuth Signature Generator ( after logging in) to generate the complete URL with Authorization header in https://dev.twitter.com/ads/reference/get/accounts/%3Aaccount_id/campaigns , it works , Twitter’s OAuth generated URL is like below
curl --get 'https://ads-api.twitter.com/0/accounts/<accountId>/campaigns'
--data 'sort_by=start_time-desc' --header 'Authorization: OAuth
oauth_consumer_key="<consumer-key>",
oauth_nonce="c313492cbabf83c8d535bf35f6fba068",
oauth_signature="<oauth-signature>",
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1430414438",
oauth_token="<access-token>",
oauth_version="1.0"' --verbose
It only FAILS when I generate the signature as described here: https://dev.twitter.com/oauth/overview/authorizing-requests
I have tests to verify the signature generated by Twitter OAuth is same as mine. Given the only factors which are changing are “oauth_nonce” which is a randomly generated alphanumeric and the “oauth_timestamp”. If I use twitter generated “ouath_nonce” and “oauth_timestamp”, my code generate the same “oauth_signature”.
I cannot make out why twitter generated ones return HTTP 200 while the authorization header generated by my code fails with HTTP 401.
I checked timestamp, they are very accurate, so I can rule out timestamp. I compared my authorization header to twitter oauth generated header, white spaces, comma, everything seem same.
Twitter generated OAuth header works, mine doesn’t although tests reveal oauth_signature generated by code is correct. Please note I am only making 1 request to fetch list of campaigns. This is not 2 legged or 3 legged approach.
I am only including Authorization header to https://ads-api.twitter.com/0/accounts/:account_id/campaigns to fetch list of campaigns. If I use the URL generated in my code, it works.
Please advise if I am missing something. Does twitter OAuth OAuth Generator do something else while generating OAuth Signature, something like whitelisting the generated OAuth Signature on the backend?
Your help is greatly appreciated!
Many Thanks,
Nirmal