您可以使用带有“服务帐户”的谷歌 API(JWT)进行服务器端身份验证。但未经用户许可直接从您的服务器上传到 youtube 服务器是不可能的。上传视频谷歌需要 OAuth2.0 身份验证。它会给你错误unAuthorized(401)- youtubeSignupRequired with "Service Account" 使用 JWT 身份验证。

由于上述限制。您可以使用以下方法来处理此问题-
- 使用客户端库获取基于 Web 的生成令牌。
- 从用户那里获取您的应用程序的 youtube 上传权限 & access_type=offline。
- 离线访问类型为您提供刷新令牌作为响应。此令牌将帮助您在其过期时继续从后端服务器令牌上传。
- 获得许可后。它将使用代码重定向到 URL。
- 使用给定的代码生成 access_token
- 保存此令牌以备将来使用。
- 使用相同的令牌将视频从您的服务器推送到 youtube 服务器
- 过期时刷新令牌。并再次执行步骤 3 - 5。
- 目前这是在 youtube 上上传视频的唯一方法。
- 在 git 存储库nodejs-upload-youtube-video-using-google-api上添加了代码
为什么它不可能?检查以下参考链接和代码:
- 来自 google API Doc:如果您尝试使用 OAuth 2.0 服务帐户流程,通常会看到此错误。YouTube 不支持服务帐户,如果您尝试使用服务帐户进行身份验证,您将收到此错误。您可以使用链接检查所有错误代码及其详细信息:YouTube 数据 API - 错误
- 来自 gadata 问题: Youtube v3 Google 服务帐户访问
- 来自谷歌博客现货:使用服务帐户支持的 Google API 列表
- 检查以下代码以从服务器端获取 access_token
您可以使用以下步骤和代码自行检查:
- 转到谷歌开发者控制台
- 创建项目
- 要获取Google+ API访问权限,请转到:APIs & Auth->APIs ->enable YouTube Data API v3
- 要启用服务帐户,请转到:APIs & Auth->Credentials->创建新的客户端 ID->单击服务帐户->创建客户端 ID
- 将机密文件保存在您的系统上并确保其安全。
- 使用您下载的以下命令和文件创建密钥:
openssl pkcs12 -in /home/rajesh/Downloads/Yourkeyfile.p12 -out youtube.pem -nodes
- Enter password: ***notasecret***
6.您可以从服务器端授权和访问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 视频列表:
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.list({auth:authClient,part:'snippet',chart:'mostPopular'}, function(err, resp) {
console.log(resp);
console.log(err);
});
});
使用服务帐户和 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' }
使用服务帐户和 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 返回以下错误:
{ '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' }
- 附上“如何获取谷歌密钥?”的屏幕截图

结论:未经用户许可上传视频是不可能的。