Brightcove API - 如何使用请求获取访问令牌?

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

Brightcove's API docs 说:

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).

这是否意味着我对 id 和 secret 单独编码,或者对整个编码?我仍然不确定我做错了什么。这是我此时的代码:

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)

这给出了错误,表明它没有获得 client_id 参数。

{"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."}

感谢您的指点。

它将 key:secret base 64 编码为单个字符串,但您可以使用请求的内置基本身份验证支持并跳过该步骤。

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)
)