google youtube api中的登录必填错误评分为

时间:2017-08-28 04:59:09

标签: javascript jquery youtube-api

我正在使用谷歌提供的代码获取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访问令牌。

1 个答案:

答案 0 :(得分:0)

JavaScript Youtube Code Samples中的示例足以让您解决此登录问题。

以下是运行示例的步骤:

  1. 在您的Google Dev Console中,确保已启用Youtube API。
  2. 为Web生成OAuth clientID,而不是API Key。您不会在此处使用API​​密钥。
  3. 复制auth.js的代码(示例文档中提到的代码)并将其保存在文本编辑器中auth.js
  4. 复制playlist_updates.js的代码并将其保存在编辑器中playlist_updates.js
  5. 复制playlist_updates.html的代码并将其保存在编辑器中playlist_updates.html
  6. 将这三个文件放在一个文件夹中。
  7. youtube_sample --
                   --auth.js
                   --playlist_updates.js
                   --playlist_updates.html
    
    1. 将生成的OAuth ClientID放入auth.js。您可以在var OAUTH2_CLIENT_ID = '__YOUR_CLIENT_ID__';
    2. 中找到将其放置在哪里
    3. 现在一切都已确定。启动本地服务器并运行它。我使用python -m SimpleHTTPServer 8000
    4. <强> 注意

      生成您将在localhost 8000(或您指定的任何端口)中运行的OauthclientID时,请在您的Google Dev Console中将其设置为:

      • 授权的JavaScript来源:http://localhost:8000
      • 授权重定向URI:http://localhost:8000/oauth2callback

      我能够运行样本:

      enter image description here

      现在您可以使用Youtube方法和其他功能。您现在应该没有问题登录。

相关问题