使用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。为此,我需要访问令牌和刷新令牌,我尝试直接获取访问令牌,但无法获取
所以,对于如何将视频直接从服务器上传到频道帐户,有什么帮助或想法吗。我在谷歌上搜索了很多节点模块,但是我找不到它
我一直在用这种方法上传视频
但是,有没有办法在不获得用户对我的应用程序的youtube上传许可的情况下实现这种方法。您可以使用带有“服务帐户”的google API(JWT)进行服务器端身份验证。但不可能在没有用户许可的情况下直接从您的服务器上传到youtube服务器。为了上传视频,谷歌需要OAuth2.0认证。它将给您错误未经授权(401)-youtubeSignupRequired使用JWT身份验证的“服务帐户”。 由于上述限制。您可以使用以下方法来处理此问题-
- 去
- 创建项目
- 要获得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);
});
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);
});
});
{ 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' }
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;
});
});
{ 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上添加了代码