簡介
獲得客戶端憑證是獲得訪問令牌的一次性先決條件,大多數Brightcove API都使用該令牌來認證請求。
有關客戶端憑據的完整說明,請參見 Brightcove OAuth服務的工作方式.
此示例向您展示如何從Web應用程序獲取客戶端憑據。 因為 OAuth API 沒有啟用CORS,並且必須從服務器端發出請求,該應用會將必要的信息發送到代理,然後由代理髮出api請求並將響應發送回客戶端。 這裡的代理是用PHP編寫的,但是任何服務器端語言都可以-您只需要能夠通過Internet向應用發送POST請求。
獲取您的BC_TOKEN
得到一個 client_id
以及 client_secret
通過 OAuth API,你需要一個 BC_TOKEN
驗證您的請求。 登錄Studio時,您的BC_TOKEN被設置為cookie。 您可以按照自己喜歡的任何方式獲取該Cookie,但是為了簡化起見,我們創建了以下JavaScript代碼段-您可以在登錄Studio時將其粘貼到開發人員控制台中,按 返回,然後會出現一個提示,其中包含BC_TOKEN:
獲取憑證應用
源代碼
在此找到與此樣本相關的所有代碼 GitHub存儲庫.
示例應用
見筆 OAuth API 樣本:創建客戶端憑據 通過Brightcove學習服務(@ rcrooks1969)上 CodePen.
使用CodePen
以下是有效使用上述CodePen的一些技巧:
- 切換 player 通過點擊 結果 按鈕。
- 點擊 HTML / CSS / JS 按鈕顯示一種代碼類型。
- 點擊 在CodePen上編輯 在右上角將此CodePen分支到您自己的帳戶中。
- 在此找到與此樣本相關的所有代碼 GitHub存儲庫.
代理代碼
為了在此頁面上構建自己的示例應用程序版本,您必須創建並託管自己的代理。 該代理與大多數示例應用程序中使用的代理有所不同,因為對 OAuth API 創建客戶端憑據的方法與常規API身份驗證的方法不同。 下面顯示了此處使用的代理的完整代碼。
<?php
/**
* client-credentials-proxy.php - proxy for Brightcove RESTful APIs
* gets a client id and client secret and returns the whole response
* Accessing:
* (note you should *always* access the proxy via HTTPS)
* Method: POST
*
* @post {string} bc_token - BC_TOKEN with admin permissions on all accounts that credentials are requested for
* @post {JSONstring} requestBody - the full request body as a JSON string
*
* @returns {string} $response - JSON response received from the OAuth API
*/
// security checks
if (strpos($_SERVER['HTTP_REFERER'], 'solutions.brightcove.com') == false && strpos($_SERVER['HTTP_REFERER'], 'ondemand.brightcovelearning.com') == false && strpos($_SERVER['HTTP_REFERER'], 'video.brightcovelearning.com') == false && strpos($_SERVER['HTTP_REFERER'], 's.codepen.io') == false && strpos($_SERVER['HTTP_REFERER'], 'fiddle.jshell.net') == false && strpos($_SERVER['HTTP_REFERER'], 'players.brightcove.net') == false && strpos($_SERVER['HTTP_REFERER'], 'support.brightcove.com') == false && strpos($_SERVER['HTTP_REFERER'], 'master-7rqtwti-6sglloa4yrkti.us.platform.sh') == false) {
exit('{"ERROR":"Only requests from http://docs.brightcove.com or http:solutions.brightcove.com are accepted by this proxy"}');
}
// CORS enablement and other headers
header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
header("X-Content-Type-Options: nosniff");
header("X-XSS-Protection");
// get data or die
if ($_POST["requestBody"]) {
$data = json_decode($_POST["requestBody"]);
} else {
exit("request body missing");
}
// get request type or default to POST
if ($_POST["requestType"]) {
$method = $_POST["requestType"];
} else {
$method = 'POST';
}
// get bc_token or die
if ($_POST["bc_token"]) {
$bc_token = $_POST["bc_token"];
} else {
exit("bc_token missing");
}
$request = "https://oauth.brightcove.com/v4/client_credentials";
$ch = curl_init($request);
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => array(
'Content-type: application/json',
"Authorization: BC_TOKEN {$bc_token}"
),
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($ch);
curl_close($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
exit('An error occurred on making the request');
} else {
echo $response;
}
?>