Hi,

I am using this request function on python.

def request(user_twurl, http_method, headers, url):
CONSUMER_KEY = user_twurl[0]
CONSUMER_SECRET = user_twurl[1]
USER_OAUTH_TOKEN = user_twurl[2]
USER_OAUTH_TOKEN_SECRET = user_twurl[3]

consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
token = oauth.Token(key=USER_OAUTH_TOKEN, secret=USER_OAUTH_TOKEN_SECRET)
client = oauth.Client(consumer, token)

header_list = {}
if headers:
    for i in headers:
        (key, value) = i.split(': ')
        if key and value:
            header_list[key] = value

response, content = client.request(url, method=http_method, headers=header_list)

try:
    data = json.loads(content)
except:
    data = None
return response, data

it works with GET commands but fails with POSTS giving the error.

{‘errors’: [{‘code’: ‘UNAUTHORIZED_ACCESS’, ‘message’: ‘This request is not properly authenticated’}], ‘request’: {‘params’: {}}}

I suspect it has to do with the body of headers. What is format of headers if I want to avoid this error?

@SaadAhmedQure10 I’m not fully sure as I don’t know what’s the actual URL you get an error. Could you please provide us the example API call including all the request parameters?

You may want to encode the url right before the request:
https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode

Hi, This an example URL that I use that works fine on terminal but fails on python .
url = “https://ads-api.twitter.com/5/stats/jobs/accounts/” + ACCOUNT_ID + “?entity=LINE_ITEM&entity_ids=” + line_item.id + “&start_time=” + start_date + “T22:00:00Z&end_time=” + end_date + “T22:00:00Z&granularity=TOTAL&placement=” + placements + “&metric_groups=BILLING”

response,data=request(user_twurl, ‘POST’, headers, url)

where user_twurl=user_twurl = [CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET]
and
headers =

@SaadAhmedQure10 Did you try urlencoding?

Hi @jrsyo,

I have tried url encoding like given below:

params=urllib.parse.urlencode({‘entity’:‘LINE_ITEM’,‘entity_ids’:‘entityid’,‘start_time’:‘2019-09-02T22:00:00Z’,‘end_time’:‘2019-09-03T22:00:00Z’,‘granularity’:‘TOTAL’,‘placement’:‘ALL_ON_TWITTER’,‘metric_groups’:‘ENGAGEMENT’})
url=“https://ads-api.twitter.com/5/stats/accounts/znvtq?%s” % params

response,data=request(user_twurl, ‘POST’, headers, url)

And Now the error I get is this

{‘errors’: [{‘code’: ‘METHOD_NOT_ALLOWED’, ‘message’: ‘Method POST not allowed.’}], ‘request’: {‘params’: {}}}

However the same parameters with POST call work on the command line.

@SaadAhmedQure10 There are two Analytics endpoints, Sync and Async. With the example from above, it seems you’re calling Sync Analytics endpoint which indeed doesn’t accept POST request, it should be GET request. Otherwise, please change the resource URL to Async endpoint so you can make a POST request as you’ve done on the command-line.

Sync: GET stats/accounts/:account_id
Async: POST stats/jobs/accounts/:account_id

1 Like

@jrsyo Yes I missed that. Seems to be working now. Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.