I am trying to utilise the authentication here: https://api.graphnethealth.com/system-auth using Python urllib3 and have the following
JavaScript
x
16
16
1
import urllib3
2
http = urllib3.PoolManager()
3
resp = http.request(
4
"POST",
5
"https://core.syhapp.com/hpca/oauth/token",
6
headers={
7
"Content-Type": "application/x-www-form-urlencoded"
8
},
9
fields={
10
"grant_type": "client_credentials",
11
"client_id": "YYYYYYYYY",
12
"client_secret": "XXXXXXXXX"
13
}
14
)
15
print(resp.data)
16
I get an error saying that grant_type
has not been sent.
JavaScript
1
2
1
b'{rn "error": {rn "code": "400",rn "message": "Validation Errors",rn "target": "/oauth/token",rn "details": [rn {rn "message": "grant_type is required",rn "target": "GrantType"rn },rn {rn "message": "Value should be one of the following password,refresh_token,trusted_token,handover_token,client_credentials,pin",rn "target": "GrantType"rn }rn ]rn }rn}'
2
Any suggestions?
Advertisement
Answer
You’re telling it the data will be form-urlencoded, but that’s not what request
does by default. I believe you need:
JavaScript
1
11
11
1
resp = http.request(
2
"POST",
3
"https://core.syhapp.com/hpca/oauth/token",
4
fields={
5
"grant_type": "client_credentials",
6
"client_id": "YYYYYYYYY",
7
"client_secret": "XXXXXXXXX"
8
},
9
encode_multipart = False
10
)
11
request
replaces the Content-Type
header, so there’s no point in specifying it at all.