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?
jrsyo
#2
@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 =
jrsyo
#5
@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.
jrsyo
#7
@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!
system
Closed
#9
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.