社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python urllib3似乎没有发送字段数据

pee2pee • 3 年前 • 1384 次点击  

我试图在这里使用身份验证: https://api.graphnethealth.com/system-auth 使用Python urllib3并具有以下功能

import urllib3
http = urllib3.PoolManager()
resp = http.request(
        "POST",
        "https://core.syhapp.com/hpca/oauth/token",
        headers={
            "Content-Type": "application/x-www-form-urlencoded"
        },
        fields={
            "grant_type": "client_credentials",
            "client_id": "YYYYYYYYY",
            "client_secret": "XXXXXXXXX"
        }
    )
print(resp.data)

我说这话是错的 grant_type 尚未发送。

b'{\r\n  "error": {\r\n    "code": "400",\r\n    "message": "Validation Errors",\r\n    "target": "/oauth/token",\r\n    "details": [\r\n      {\r\n        "message": "grant_type is required",\r\n        "target": "GrantType"\r\n      },\r\n      {\r\n        "message": "Value should be one of the following password,refresh_token,trusted_token,handover_token,client_credentials,pin",\r\n        "target": "GrantType"\r\n      }\r\n    ]\r\n  }\r\n}'

有什么建议吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131216
 
1384 次点击  
文章 [ 2 ]  |  最新文章 3 年前
pachink0
Reply   •   1 楼
pachink0    3 年前

这是因为你指定的错误 Content-Type 标题值。请求主体是JSON,请尝试 Content-Type: application/json .

Tim Roberts
Reply   •   2 楼
Tim Roberts    3 年前

你告诉它,数据将以URL编码的形式出现,但事实并非如此 request 默认情况下是这样的。我相信你需要:

resp = http.request(
        "POST",
        "https://core.syhapp.com/hpca/oauth/token",
        fields={
            "grant_type": "client_credentials",
            "client_id": "YYYYYYYYY",
            "client_secret": "XXXXXXXXX"
        },
        encode_multipart = False
    )

要求 取代 Content-Type 标题,所以根本没有必要指定它。