Python请求POST错误400
我有以下代码:Python请求POST错误400,python,python-requests,Python,Python Requests,我有以下代码: import requests import json data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"} url = 'http://MYURL/connect/token' response = requests.post(url, json=data, verify=False) pri
import requests
import json
data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"}
url = 'http://MYURL/connect/token'
response = requests.post(url, json=data, verify=False)
print(response)
print response.reason
print(response.json())
我正试图在测试环境中测试与一个新的身份验证服务的连接(这就是为什么verify为FALSE),这将为我提供访问令牌和令牌类型,并使用它们可以发布到API
但我总是得到:
<Response [400]> Bad Request {u'error': u'invalid_request'}
错误请求{u'error':u'invalid_Request'}
我不确定是什么问题?
为什么这是一个错误的请求?也许您需要设置标题的内容类型
import requests
import json
data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"}
headers = {'content-type': 'application/json'}
url = 'http://MYURL/connect/token'
response = requests.post(url, json=data, verify=False, headers=headers)
也许您需要设置标题的内容类型
import requests
import json
data={"client_id" : "a", "client_secret" : "thisissecret", "grant_type" : "clientcredentials", "scope" : "PublicApi"}
headers = {'content-type': 'application/json'}
url = 'http://MYURL/connect/token'
response = requests.post(url, json=data, verify=False, headers=headers)
看起来您正在尝试使用客户端凭据授权获取OAuth 2.0访问令牌。这是 我在这里看到两个问题:
- 您必须将字段发布为
,而不是application/x-www-form-urlencoded
。为此,请使用json
request.post()的
参数,而不是data
json
值必须是grant\u type
而不是client\u credentials
clientcredentials
import requests
data = {"client_id" : "a", "client_secret" : "thisissecret",
"grant_type" : "client_credentials", "scope" : "PublicApi"}
url = 'http://MYURL/connect/token'
response = requests.post(url, data=data, verify=False)
if response.ok:
print(response.json())
看起来您正在尝试使用客户端凭据授权获取OAuth 2.0访问令牌。这是 我在这里看到两个问题:
- 您必须将字段发布为
,而不是application/x-www-form-urlencoded
。为此,请使用json
request.post()的
参数,而不是data
json
值必须是grant\u type
而不是client\u credentials
clientcredentials
import requests
data = {"client_id" : "a", "client_secret" : "thisissecret",
"grant_type" : "client_credentials", "scope" : "PublicApi"}
url = 'http://MYURL/connect/token'
response = requests.post(url, data=data, verify=False)
if response.ok:
print(response.json())
如果可能,发布“”终结点code@RanaAhmed嗨,url本身是好的。我和邮递员测试了同样的请求,结果成功了。我怀疑这与我如何构建这段代码有关……这可能有用吗?或者,您可能希望将
json=data
更改为json=json.dumps(data)
,但请先调用import json
。@coldspeed如果可能,在“”端点之后仍然会出现相同的错误code@RanaAhmed嗨,url本身是好的。我和邮递员测试了同样的请求,结果成功了。我怀疑这与我如何构建这段代码有关……这可能有用吗?或者,您可能希望将json=data
更改为json=json.dumps(data)
,但首先调用import json
。@coldspeed同样的错误仍然出现requests
当您使用json={“your”:“data”}
参数来requests.post()
时,您将解决这个问题。From:在请求中使用json参数将把标题中的内容类型更改为application/json。
请求
将在您使用json={“您的”:“数据”}
参数时为您解决这一问题。requests.post()
不知道。From:在请求中使用json参数将把标题中的内容类型更改为application/json。
回溯(最近一次调用):文件“import.py”,第13行,if response.is_ok:AttributeError:“response”对象没有属性“is_ok”打字错误,现已修复!它起作用了。如何访问response.json中的信息?response.json()
返回一个标准的Python dict(这是解析json响应的结果),因此您可以像dict一样读取它(例如:获取访问令牌:at=response.json()['access\u token']
),谢谢!我会做+1,但我没有足够的秩。回溯(最近一次调用最后一次):文件“import.py”,第13行,在if response.is_ok:AttributeError:“response”对象没有属性“is_ok”打字错误,现已修复!它起作用了。如何访问response.json中的信息?response.json()
返回一个标准的Python dict(这是解析json响应的结果),因此您可以像dict一样读取它(例如:获取访问令牌:at=response.json()['access\u token']
),谢谢!我会做+1,但我没有足够的排名。