【发布时间】:2016-03-07 05:04:18
【问题描述】:
我正在尝试从 The Trade Desk 的(沙盒)api 获取身份验证令牌,但我收到了 400 响应,说明:
“将 Content-Type 'application/json' 读取为 JSON 时出错:意外 解析值时遇到的字符:L. Path '', line 0, 位置 0。”
整个response.json():
{u'ErrorDetails': [{u'Reasons': [u"Error reading Content-Type 'application/json' as JSON: Unexpected character encountered while parsing value: L. Path '', line 0, position 0."], u'Property': u'TokenRequest'}], u'Message': u'The request failed validation. Please check your request and try again.'}
我的脚本(可运行):
import requests
def get_token():
print "Getting token"
url = "https://apisb.thetradedesk.com/v3/authentication"
headers = {'content-type': 'application/json'}
data = {
"Login":"logintest",
"Password":"password",
"TokenExpirationInMinutes":60
}
response = requests.post(url, headers=headers, data=data)
print response.status_code
print response.json()
return
get_token()
我相信这意味着我的 headers var 没有被 requests 正确序列化,这似乎是不可能的,或者没有被 The Trade Desk 正确反序列化。我已经进入了 requests 库,但我似乎无法破解它并正在寻找其他输入。
【问题讨论】:
-
数据需要拨打
json.dump吗? -
宾果游戏,就是这样,让它工作。你能解释一下吗?
-
data本身就是一个python字典;它不是json格式。调用json.dumps会将其转换为 json。 -
我在下面回答了,但是 john 比我快很多。
-
谢谢大家,非常感谢。我会接受你的回答@ferdy
标签: python json python-requests