我希望能够检测当前经过身份验证的用户是否订阅了YouTube API v3中的特定YouTube频道。
可能的解决方案是检索当前已验证用户的所有订阅的列表,并检查该列表中是否包含该频道的频道ID。那将是一个非常低效的解决方案,如果用户有数百个订阅,可能需要很长时间。
有没有简单的方法来检查这个?我查看了整个API文档,但找不到任何内容。
答案 0 :(得分:10)
使用subscriptions#list方法,并在mine
中传递forChannelId
= true和要检查的频道ID。如果经过身份验证的用户未订阅该频道,则会返回一个空列表。
答案 1 :(得分:0)
checkSubscribe (params) {
var request =
gapi.client.youtube.subscriptions.list(removeEmptyParams(params));
request.execute((response) => {
console.log(response);
if (response.code !== 401 && response.code !== 400 && response.items[0] ) {
console.log('response');
console.log(response);
}
});
}
removeEmptyParams(params)[![enter image description here][1]][1]{
for (const pra in params) {
if (!params[pra] || params[pra] === 'undefined') {
delete params[pra];
}
}
return params;
}
checkSubscribe(
{part: 'snippet, contentDetails', mine: true},
{'forChannelId':'PUT-YOUR-CHANEL--ID','onBehalfOfContentOwner': ''}
);