使用node.js直接从服务器使用youtube/google API上传视频?

使用node.js直接从服务器使用youtube/google API上传视频?,node.js,youtube,google-api,youtube-api,google-api-nodejs-client,Node.js,Youtube,Google Api,Youtube Api,Google Api Nodejs Client,我正在尝试从服务器上传视频,而不需要客户端的用户进行任何手动验证。我尝试了下面的视频上传代码片段,但它在浏览器中验证了用户身份,并请求接受该应用程序 var ResumableUpload = require('node-youtube-resumable-upload'); var googleauth = require('google-auth-cli'); var google = require('googleapis'); var getTokens = function(call

我正在尝试从服务器上传视频,而不需要客户端的用户进行任何手动验证。我尝试了下面的视频上传代码片段,但它在浏览器中验证了用户身份,并请求接受该应用程序

var ResumableUpload = require('node-youtube-resumable-upload');
var googleauth = require('google-auth-cli');
var google = require('googleapis');

var getTokens = function(callback) {
  googleauth({
      access_type: 'offline',
      scope: 'https://www.googleapis.com/auth/youtube.upload' //can do just 'youtube', but 'youtube.upload' is more restrictive
  },
  {     client_id: CLIENT_ID, //replace with your client_id and _secret
      client_secret: CLIENT_SECRET,
      port: 3000
  },
  function(err, authClient, tokens) {
    console.log(tokens);
    callback(tokens);
  });
};

getTokens(function(result) {
  tokens = result;
  upload();
});



var upload = function() {
      var metadata = {snippet: { title: 'title', description: 'Uploaded with ResumableUpload' },
          status: { privacyStatus: 'public' }};
      var resumableUpload = new ResumableUpload(); //create new ResumableUpload
      resumableUpload.tokens = tokens;
      resumableUpload.filepath = 'youtube/test4.mp4';
      resumableUpload.metadata = metadata;
      resumableUpload.monitor = true;
    resumableUpload.eventEmitter.on('progress', function(progress) {
        console.log(progress);
    });
      resumableUpload.initUpload(function(result) {
        console.log(result);
        return;
      });
    }
但对于我的应用程序,它应该直接从服务器上传视频到youtube。为此,我需要访问令牌和刷新令牌,我尝试直接获取访问令牌,但无法获取

所以,对于如何将视频直接从服务器上传到频道帐户,有什么帮助或想法吗。我在谷歌上搜索了很多节点模块,但是我找不到它

我一直在用这种方法上传视频

  • 使用客户端库获取基于web生成的令牌
  • 正在从用户处获取我的应用程序的youtube上载权限& 访问类型=脱机
  • Access type offline提供刷新令牌作为响应。这个令牌 将有助于在其 过期
  • 得到许可后。它将用代码重定向到URL
  • 使用给定的代码生成访问令牌
  • 保存此令牌以备将来使用
  • 使用相同的令牌将视频从服务器推送到youtube 服务器
  • 当令牌过期时刷新令牌

  • 但是,有没有办法在不获得用户对我的应用程序的youtube上传许可的情况下实现这种方法。

    您可以使用带有“服务帐户”的google API(JWT)进行服务器端身份验证。但不可能在没有用户许可的情况下直接从您的服务器上传到youtube服务器。为了上传视频,谷歌需要OAuth2.0认证。它将给您错误未经授权(401)-youtubeSignupRequired使用JWT身份验证的“服务帐户”。

    由于上述限制。您可以使用以下方法来处理此问题-

  • 使用客户端库获取基于web生成的令牌
  • 从用户处获取应用程序的youtube上载权限&access\u type=offline
  • Access type offline为您提供刷新令牌作为响应。此令牌将帮助您在其过期时继续从后端服务器上载令牌
  • 得到许可后。它将用代码重定向到URL
  • 使用给定的代码生成访问令牌
  • 保存此令牌以备将来使用
  • 使用相同的令牌将视频从服务器推送到youtube服务器
  • 当令牌过期时刷新令牌。然后再次执行步骤3-5
  • 目前,这是在youtube上上传视频的唯一方式
  • 在git存储库中添加了代码
  • 为什么不可能呢?检查以下参考链接和代码:

  • 来自google API文档:如果您尝试使用OAuth 2.0服务帐户流,通常会出现此错误YouTube不支持服务帐户,如果您尝试使用服务帐户进行身份验证,将出现此错误。您可以使用以下链接检查所有错误代码及其详细信息:
  • 来自《伽达达》杂志:
  • 来自谷歌博客spot:
  • 检查以下代码以从服务器端获取访问令牌
  • 您可以使用以下步骤和代码自行检查:

    • 创建项目
    • 要获得Google+API访问权限,请访问:API&Auth->API->启用YouTube数据API v3
    • 要启用服务帐户,请转到:API&Auth->Credentials->Create new Client ID->单击服务帐户->Create Client ID
    • 在系统上保存机密文件并确保其安全
    • 使用下载的以下命令和文件创建密钥:
  • openssl pkcs12-in/home/rajesh/Downloads/Yourkeyfile.p12-out youtube.pem-nodes

    - Enter password: ***notasecret***
    
    六,。您可以从服务器端授权和访问api,如下所示:

        var google = require('googleapis');
        var authClient = new google.auth.JWT(
                'Service account client email address', #You will get "Email address" in developer console for Service Account:
                'youtube.pem', #path to pem file which we create using step 6
                null,
                ['https://www.googleapis.com/auth/youtube.upload'],
                null
        );
        authClient.authorize(function(err, tokens) {
           if (err) {
                   console.log(err);
                   return;
           }
           console.log(tokens);
        });
    
  • 使用服务帐户获取youtube视频列表(工作):

  • 使用服务帐户和googleapis模块插入youtube视频:

         var google = require('googleapis');
         var youtube = google.youtube('v3');
         var authClient = new google.auth.JWT(
              'Service account client email address', #You will get "Email address" in developer console for Service Account:
              'youtube.pem',
              null,
           ['https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.upload'],
           null
         );
        authClient.authorize(function(err, tokens) {
            if (err) {
               console.log(err);
               return;
        }
          youtube.videos.insert({auth:authClient,part:'snippet,status,contentDetails'},function(err,resp)
           console.log(resp);
           console.log(err);
         });
        });
    
  • 插入/上载API返回以下错误:

    { errors: 
       [ { domain: 'youtube.header',
           reason: 'youtubeSignupRequired',
           message: 'Unauthorized',
           locationType: 'header',
           location: 'Authorization' } ],
      code: 401,
      message: 'Unauthorized' }
    
       { 'www-authenticate': 'Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token',
      'content-type': 'application/json; charset=UTF-8',
      'content-length': '255',
      date: 'Tue, 16 Sep 2014 10:21:53 GMT',
      server: 'UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")',
      'alternate-protocol': '443:quic,p=0.002' }
    
  • 使用服务帐户和ResumableUpload模块插入youtube视频:

         var google = require('googleapis');
         var ResumableUpload = require('node-youtube-resumable-upload');
         var authClient = new google.auth.JWT(
              'Service account client email address', #You will get "Email address" in developer console for Service Account:
              'youtube.pem',
              null,
           ['https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.upload'],
           null
         );
        authClient.authorize(function(err, tokens) {
            if (err) {
               console.log(err);
               return;
        }
          var metadata = {snippet: { title: 'title', description: 'Uploaded with ResumableUpload' },status: { privacyStatus: 'private' }};
          var resumableUpload = new ResumableUpload(); //create new ResumableUpload
          resumableUpload.tokens = tokens;
          resumableUpload.filepath = 'youtube.3gp';
          resumableUpload.metadata = metadata;
          resumableUpload.monitor = true;
          resumableUpload.eventEmitter.on('progress', function(progress) {
               console.log(progress);
          });
          resumableUpload.initUpload(function(result) {
               console.log(result);
               return;
          });
    
        });
    
  • 插入/上载API返回以下错误:

    { errors: 
       [ { domain: 'youtube.header',
           reason: 'youtubeSignupRequired',
           message: 'Unauthorized',
           locationType: 'header',
           location: 'Authorization' } ],
      code: 401,
      message: 'Unauthorized' }
    
       { 'www-authenticate': 'Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token',
      'content-type': 'application/json; charset=UTF-8',
      'content-length': '255',
      date: 'Tue, 16 Sep 2014 10:21:53 GMT',
      server: 'UploadServer ("Built on Aug 18 2014 11:58:36 (1408388316)")',
      'alternate-protocol': '443:quic,p=0.002' }
    
  • “如何获取谷歌密钥?”

  • 结论:不可能在没有用户许可的情况下上载视频。

    authClient.authorize(函数(err,令牌){if(err){console.log(err);return;}console.log(令牌);})
    此函数引发一个错误,如error:enoint,open'C:\myapp\Youtube\Youtube.pem'抱歉,我解决了该错误,但它显示另一个错误,如{error:'invalid_grant'}eNote:表示找不到文件或无法读取文件。请交叉验证上面提到的google应用程序设置以及文件路径。只有当您未使用正确的google凭据(如电子邮件id)时,才会出现无效授权。请查看第三个屏幕截图以了解详细信息this@sathishkumar添加了youtube api示例,包括ResumableUpload。检查响应。@在git上添加了代码