Python urllib3 似乎没有发送字段数据
Python urllib3 doesn't seem to be sending fields data
我正在尝试在此处使用身份验证: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}'
有什么建议吗?
您告诉它数据将是 form-urlencoded,但默认情况下 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
)
request
替换了 Content-Type
header,因此根本没有必要指定它。
这是因为您指定了错误的 Content-Type
header 值。请求 body 是 JSON,所以尝试使用 Content-Type: application/json
。
我正在尝试在此处使用身份验证: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}'
有什么建议吗?
您告诉它数据将是 form-urlencoded,但默认情况下 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
)
request
替换了 Content-Type
header,因此根本没有必要指定它。
这是因为您指定了错误的 Content-Type
header 值。请求 body 是 JSON,所以尝试使用 Content-Type: application/json
。