pythonapibrightcove

Brightcove API - how to get an access token using requests?


Brightcove's API docs say:

Authorization: Basic {client_id}:{client_secret}

The entire {client_id}:{client_secret} string must be Base64-encoded (curl will automatically Base64-encode the string if you pass it as --user credentials; in other languages, you'll need to handle the Base64-encoding yourself).

Does this mean I encode the id and secret individually, or the whole thing? I'm still not sure what I'm doing wrong. Here is my code at this point:

id_key = b64encode('myid12345qwerty'.encode()).decode("utf-8")
secret_key = b64encode('mysecret12345qwerty67890'.encode()).decode("utf-8")
creds = '{'+id_key+':'+secret_key+'}'
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + creds}

r = requests.post('https://oauth.brightcove.com/v4/access_token?grant_type=client_credentials', headers = headers)
print(r.status_code)
print(r.text)

This gives the error that says it isn't getting the client_id parameter.

{"error":"invalid_client","error_description":"The "client_id" parameter is missing, does not name a client registration that is applicable for the requested call, or is not properly authenticated."}

Thanks for any pointers here.


Solution

  • It would be key:secret base 64 encoded as a single string, but you can use request's built in support for basic auth and skip that step.

    id_key = 'myid12345qwerty'
    secret_key = 'mysecret12345qwerty67890'
    r = requests.post(
      'https://oauth.brightcove.com/v4/access_token?grant_type=client_credentials',
      auth=(id_key, secret_key)
    )