2

I'm trying to request information from an API. The way I'm passing in the OAUTH Token is wrong, I assume.

import requests
import json

URL = "https://api.direct.yandex.com/json/v5/keywords"
token = "/* Access Token */"

PARAMS = {
           'Authorization': "Bearer " + token,
           'Accept-Language': "en",
           'processingMode': "auto",
}
BODY = {
           'method': "CreateNewWordstatReport",
           'param': {
                "Phrases": ['pipeline'],
                "GeoID": [1,-219]
                }
           }

#jdata = json.dumps(PARAMS, ensure_ascii=False).encode ('utf8')

body = json.dumps(BODY, indent=4)
response = requests.post(URL, body, headers=PARAMS)
response.encoding = 'utf-8'

#response = requests.get(url = URL, params = PARAMS)

print(response.status_code)
print(response.url)
print(response.json())

The Commented Lines are potential alternatives to the three lines in-between. How do I use .post() and .get() correctly here, in order to pass in the token? The current response is shown:

202

https://api.direct.yandex.com/json/v5/keywords

{'error': {'request_id': '1891/* some more numbers */0199', 'error_code': 8000, 'error_detail': 'Not able to process JSON/XML', 'error_string': 'Invalid request'}}

Thanks much for any help!

mvcast77
  • 21
  • 1

1 Answers1

0

Have you tried the same in Postman? If not, would recommend to try the same there and use the "Code" option to get built code sample. From one of the samples, here is the code for passing Oauth token:

import requests
import json

url = "<url>"

payload = json.dumps({<Body>
})
headers = {
  'Accept-Charset': 'utf-8',
  'Accept-Encoding': 'utf-8',
  'Content-Type': 'application/json',
  'Authorization': 'Bearer <token-value>'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
Srinath Menon
  • 1,479
  • 8
  • 11