我正在尝试从API请求信息。我认为我传递OAUTH令牌的方式是错误的。
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())
注释行是介于两者之间的三行的潜在替代品。如何在这里正确使用.post((和.get((来传递令牌?显示当前响应:
202
https://api.direct.yandex.com/json/v5/keywords
{'error':{'request_id':"1891/*更多数字*/0199","error_code":8000,"error_detail":"无法处理JSON/XML","error _string":"无效请求"}}
非常感谢您的帮助!
你在Postman中也尝试过吗?如果没有,建议在那里尝试同样的方法,并使用";代码";选项来获取生成的代码示例。从其中一个示例中,以下是传递Oauth令牌的代码:
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)