我正在使用谷歌提供的代码获取youtube视频的评级,以便在网站内使用。这是代码
function videosGetRating(params) {
gapi.client.setApiKey('xxxxxxxxxxxxxxxxxxxxxx');
gapi.client.load('youtube', 'v3', function() {
params = removeEmptyParams(params);
var request = gapi.client.youtube.videos.getRating(params);
executeRequest(request);
});
}
$('#idlikes').click(function(){
videosGetRating({'id': '6M6W2lbkuiw',
'onBehalfOfContentOwner': ''});
});
但我收到了错误
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
我不知道为什么要求身份验证,但gapi.client.setApiKey只需要api密钥。如果我没有错,当oauth2访问令牌没有传递给api密钥时会出现此错误。但是没有选择在此代码中添加oauth2访问令牌。
答案 0 :(得分:0)
JavaScript Youtube Code Samples中的示例足以让您解决此登录问题。
以下是运行示例的步骤:
auth.js
playlist_updates.js
playlist_updates.html
youtube_sample -- --auth.js --playlist_updates.js --playlist_updates.html
auth.js
。您可以在var OAUTH2_CLIENT_ID = '__YOUR_CLIENT_ID__';
python -m SimpleHTTPServer 8000
<强> 注意 强>
生成您将在localhost 8000(或您指定的任何端口)中运行的OauthclientID时,请在您的Google Dev Console中将其设置为:
http://localhost:8000
http://localhost:8000/oauth2callback
我能够运行样本:
现在您可以使用Youtube方法和其他功能。您现在应该没有问题登录。