'error 500 when tried to POST API with requests

I tried to post data to external API with requests, here's my code :

import requests
import json

client_id = "MY_CLIENT_ID"
secret = "MY_CLIENT_SECRET"
data = {
    'grant_type': 'client_credentials',
    'scope': 'MY_SCOPES',
}
head_token = {
    "Client_id": client_id,
    "Client_secret": secret,
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic SOME-RANDOM-STRING"
}
response = requests.post(
    "https://api.example.com/oauth2/token", data=data, headers=head_token)


print(response.status_code)

if response.status_code in [200]:
    resp_dict = json.loads(response.text)
    access_token = resp_dict["access_token"]
    print(access_token)

    url = 'https://api.example.com/send/data'
    data = {'some' : 'data'}
    headers = {
        'client_id': client_id,
        'client_secret': secret,
        'authorization': "Bearer %s"  %access_token,
        'content-type': "application/json",
        'accept': "application/json"
    }

    r = requests.post(url, data=json.dumps(
        data, default=str), headers=headers)
    print(r.status_code)

the script succeed to get the access token, but when it hit the 2nd endpoit it always give 500 response. heres the terminal :

python3 test_hit.py
200
THE_ACCESS_CODE
500

I want to know what is the caused it give me response 500 even though I already get the access token to hit the 2nd one?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source