sat*_*mar 2 youtube google-api youtube-api node.js google-api-nodejs-client
我正在尝试从服务器上传视频而无需用户在客户端进行任何手动身份验证.我尝试使用以下代码片段进行视频上传,但它会在浏览器中对用户进行身份验证,并要求接受该应用.
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;
});
}
Run Code Online (Sandbox Code Playgroud)
但对于我的应用程序,它应该直接从服务器上传视频到youtube.为此我需要访问令牌和刷新令牌我尝试了很多直接获取访问令牌但我无法得到它.
所以任何有关如何将视频直接从服务器上传到频道帐户的帮助或想法.我在谷歌搜索了一个节点模块,但我无法找到它.
我一直用这种方法上传视频
但有没有办法实现这种方法,而无需获得用户对我的应用程序的youtube上传权限.
raj*_*ade 12
您可以使用带有"服务帐户"的Google API(JWT)进行服务器端验证.但是无法在没有用户许可的情况下从您的服务器直接上传到YouTube服务器.要上传视频,Google需要OAuth2.0身份验证.它将使用JWT身份验证为您提供错误unAuthorized(401) - youtubeSignupRequired with"Service Account".

由于上述限制.您可以使用以下方法来处理此问题 -
为什么它不可能?检查以下参考链接和代码:
您可以使用以下步骤和代码自行检查:
openssl pkcs12 -in /home/rajesh/Downloads/Yourkeyfile.p12 -out youtube.pem -nodes
- Enter password: ***notasecret***
Run Code Online (Sandbox Code Playgroud)
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);
});
Run Code Online (Sandbox Code Playgroud)
使用服务帐户获取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);
});
});
Run Code Online (Sandbox Code Playgroud)使用服务帐户和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);
});
});
Run Code Online (Sandbox Code Playgroud)插入/上传API返回错误:
{ errors:
[ { domain: 'youtube.header',
reason: 'youtubeSignupRequired',
message: 'Unauthorized',
locationType: 'header',
location: 'Authorization' } ],
code: 401,
message: 'Unauthorized' }
Run Code Online (Sandbox Code Playgroud)
使用服务帐户和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;
});
});
Run Code Online (Sandbox Code Playgroud)插入/上传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' }
Run Code Online (Sandbox Code Playgroud)

结论:无法在没有用户许可的情况下上传视频.
| 归档时间: |
|
| 查看次数: |
5368 次 |
| 最近记录: |