我的目标是通过传递视频ID从Brightcove CMS API获取有关视频的元数据。
我们的视频元数据存储在四个不同的业务单元(或概要文件)中,因此我必须分别查询每个业务单元的端点。我不知道在哪个业务单位会找到哪个视频ID。我每个电话最多可以发送10个视频I。下面是通过视频ID获取视频元数据所需的步骤。
1)获取access token (此步骤使用我正在提供的客户端凭据)。
2)通过token param通过access token请求对象(sendRequest)。
3)分析响应并将其放入全局videosArray中。
4)对每组10个唯一的视频ID(由多维数组表示)重复步骤1至3。
5)对每个视频帐户(业务单位)重复步骤1至4。
FYI:Brightcove的样板代码使用回调。我已经将该代码转换为异步/等待。如果异步/等待代码不正确,也许有人也可以建议对它进行改进。
我使用的是Node 8.10、ES6+和模块(以及其他模块)。似乎被隐式声明的任何变量都在全局范围中声明。我只是没把它们粘在这里。
bizUnit是这样的对象数组(我总共有四个不同的业务单元要迭代):
businessUnits = [
bizUnitOne: {
account_id: 'uhdafoia98243r2',
client_id: 'oidahf982y229hr',
client_secret: 'iuahf9o4398oyg',
player_url: 'afdhy984wyyfsg',
},
bizUnitTwo: {
account_id: 'uhdafoia98243r2',
client_id: 'oidahf982y229hr',
client_secret: 'iuahf9o4398oyg',
player_url: 'afdhy984wyyfsg',
}
]获取访问令牌函数声明:
async function getAccessToken(bizUnit) {
// base64 encode the client_id:client_secret string for basic auth
let bodyObj,
token;
authString = new Buffer(bizUnit.client_id + ':' + bizUnit.client_secret).toString('base64');
let payLoad = {
method: 'POST',
url: 'https://oauth.brightcove.com/v3/access_token?grant_type=client_credentials',
headers: {
'Authorization': 'Basic ' + authString,
'Content-Type': 'application/json'
},
json: true
};
try {
let result = await request(payLoad);
bodyObj = await JSON.parse(result);
token = bodyObj.access_token;
return token;
}
catch (error) {
console.log(oauthError, error);
}}
发送请求声明:
async function sendRequest(options) {
let requestOptions = {
method: 'GET',
url: options.url,
headers: {
'Authorization': 'Bearer ' + options.token,
'Content-Type': 'application/json'
},
json: true
};
let makeRequest = async (reqOptions) => {
try {
let body = await request(reqOptions);
return JSON.parse(body);
} catch (error) {
console.log(apiError, error);
}
};
// make the request
await makeRequest(requestOptions);}
视频in数组的示例(理论上,这个数组可以以10块或更少的块包含数百或数千个视频in):
videoIdsGroup = [ [53245,2352,243252,2352352,234234,234324,2342342,24242,23542,234324], [43534, 34543, 3453, 3453345] ];把这一切汇集在一起并提出要求:
function setUpVideoRequest(bizUnit) {
(async (bu) => {
// note that access tokens live for 5 minutes
// but you can always request one for each call to be safe
for (let videoIdsArr of videoIdsGroup) {
let endPoint,
videoIdsString = videoIdsArr.join();
endPoint = '/accounts/' + bu.account_id + '/videos/' + videoIdsString + '&sort=' + sort;
options.url = baseURL + endPoint;
options.token = await getAccessToken(bizUnit);
const videos = await sendRequest(options);
videosArray = videosArray.concat(videos);
}
})(bizUnit);}
启动执行:
for (let bu of businessUnits) {
/*the counter below is to reveal how many times and in what sequence this for...of loop is executing. my console.log indicates that this loop iterates through all businessUnits immediately, but then it runs again and eventually succeeds in some of the calls and repeats those calls even though it already got data.*/
let parentCounter = 0;
console.log("BizUnit COUNTER", parentCounter++);
try {
promises.push(setUpVideoRequest(bu));
} catch (error) {
throw error;
}}
我得到的错误消息(这个重复,然后偶尔得到一些数据,然后错误再次重复):
statusCode: 400,
message: '400 - {"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."}',我知道for...of循环没有正确运行,但是我用i迭代器尝试了正则for循环,我也尝试过for...in,我尝试过forEach。他们都不能正常工作。
我只希望能够向每个帐户ID (业务单位)和每个视频数组发出请求,并获得所有视频元数据,并通过如下方式将其放入全局videosArray中:
Promise.all(promises).then((results) => {
promiseVidArray.push(results);
});提前感谢您的帮助和洞察力。
发布于 2018-09-21 07:15:44
在businessUnits对象中缺少一个结束括号
businessUnits = [{
bizUnitOne: {
account_id: 'uhdafoia98243r2',
client_id: 'oidahf982y229hr',
client_secret: 'iuahf9o4398oyg',
player_url: 'afdhy984wyyfsg',
},
bizUnitTwo: {
account_id: 'uhdafoia98243r2',
client_id: 'oidahf982y229hr',
client_secret: 'iuahf9o4398oyg',
player_url: 'afdhy984wyyfsg',
}
}]; // <-- Here it is the one you're missing发布于 2018-09-21 20:20:45
在对异步/等待语法(谢谢@Bergi)做了一些更正之后,我发现了问题的根源:我为4个请求中的3个发送了不正确的客户端凭据。原因很有趣。我已经在下面详述过了。
在Brightcove视频云中创建API身份验证密钥时,为密钥提供一个名称,选择要在该密钥下授权的业务段,并选择希望在这些业务单元下公开的端点。
我为每个业务单位创建了一个密钥,Brightcove为每个密钥提供了一个惟一的“account_id”、“client_id”、“client_secret”。我立即将它们复制/粘贴到记事本文件中,因为'client_secret‘在5分钟后消失,无法检索(您必须删除该键并创建一个新的键)。
我今天登录到Brightcove管理面板以确认我的钥匙是否正确。令我惊讶的是,所有四个'client_ids‘都是完全相同的(我复制/粘贴的客户机I是唯一的)。在我的API请求中,我向每个业务单元发送了一个唯一的“client_id”,因为这是Brightcove在创建API身份验证密钥时提供给我的。不知何故,它们都发生了变化,成为了所有四个业务单位的完全相同的关键。我还是不明白为什么或者怎么会发生这种事。
因此,我的要求在4个账户中有3个没有得到满足。
https://stackoverflow.com/questions/52374572
复制相似问题