Skip to content
Advertisement

Python urllib3 doesn’t seem to be sending fields data

I am trying to utilise the authentication here: https://api.graphnethealth.com/system-auth using Python urllib3 and have the following

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)

I get an error saying that grant_type has not been sent.

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}'

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:

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 replaces the Content-Type header, so there’s no point in specifying it at all.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement