我正在try 获取特定API的访问令牌,下面给出了curl命令:

    curl --location --request POST 'https://example.com/oauth/connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=abcd' \
--data-urlencode 'client_secret=abc' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=us' \
--data-urlencode 'scope=a' \
--data-urlencode 'password=abc'

我有以下python代码:

url = 'https://example.com/oauth/connect/token'
body = {'client_id':'abcd', 
'client_password':'abc', 
'grant_type':'password',
'username':'us',  
'scope':'a',
'password':'abc'}
headers = {'Content-Type':'application/x-www-form-urlencoded'}

response = requests.post(url, data=body, headers=headers, verify=False)
print(response,response.content)

我一直收到<Response [400]> b'{"error":"invalid_client"}'个错误.

我相信我已经try 了几乎所有的东西,我迷路了.如果我遗漏了一些非常简单的东西,请告诉我.

推荐答案

我注意到,您的curl命令使用client_secret,而您的requests命令使用client_password.

对于调试此类请求,我建议使用https://www.toptal.com/developers/postbin/这样的免费页面,您可以在其中发送curlrequests消息,并比较到达端点的内容.

Python-3.x相关问答推荐

math. gcd背后的算法是什么,为什么它是更快的欧几里得算法?

如何从Django连接到Neo4J s AuraDB(免费层)?'

Pandas 数据帧断言等同于NaN

Pandas -我们如何在一行中应用多个要求

新行是pandas数据帧中旧行的组合

pip install saxonche v 12.1.0 产生 FileNotFoundError

pytorch 中 mps 设备的 manual_seed

SMTP 库 Python3:不太安全的应用程序访问

从日志(log)文件中查找延迟最低的用户

使用 Python 解析 JSON 嵌套字典

Pandas 将列格式化为货币

从 Python2 到 Python3 的这种解包行为的变化是什么?

你如何表达一个没有参数的 Python Callable?

Visual Studio Code 中的 Python 3.x 类型提示

pythondecorator中的变量范围

如何使用 asyncio 添加连接超时?

为什么中断比引发异常更快?

在 WSL (Ubuntu) 中为 python3 安装 venv

当默认 pip 为 pip2 时,升级 pip3 的正确格式是什么?

如何使用 python http.server 运行 CGI hello world