如何使用 node js googleapis 客户端上传和更新 youtube 频道横幅
How to upload and update youtube channel banner using node js googleapis client
我正在编写一个上传和更新 youtube 频道横幅的应用程序。我正在使用 node.js 和 google api 客户端。
在官方 api 文档中没有关于 node.js 如何发送图像内容的示例,也没有关于回调签名的信息
这是我的代码:
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2();
var youtube = google.youtube({ version: 'v3' });
//Setting the credentials
oauth2Client.setCredentials({
access_token: 'STORED_IN_DATABASE',
refresh_token: 'STORED_IN_DATABASE',
});
//Send the request
youtube.channelBanners.insert({
auth: oauth2Client,
//image_content
}, callback);
调用insert方法后,还要调用channels.update方法,也没有node.js
上的例子
从 Channel banners insert,您必须致电:
channelBanners.insert
,并从响应 中获取url字段
channels.update
与之前检索到的 URL
您还需要 channels.update
请求的频道 ID。还要注意 from Channel update :
If you are submitting an update request, and your request does not specify a value for a property that already has a value, the property's existing value will be deleted.
因此您可能需要调用 channels.list
来获取要更新的 brandingSettings
部分的通道对象
与 google-api-nodejs-client 的 API 通话:
-
youtube.channelBanners.insert({
media: {
mimeType: "image/jpeg",
body: fs.createReadStream('banner.jpeg')
}
}, function(err, uploadResponse, response) {
});
可用的 mimeType
是 image/jpeg
、image/png
、application/octet-stream
-
youtube.channels.list({
part: "brandingSettings",
mine: true
}, function(err, channelListRsp, response) {
});
-
channelListRsp.items[0].brandingSettings.image.bannerExternalUrl = uploadResponse.url;
youtube.channels.update({
part: "brandingSettings",
resource: channelListRsp.items[0]
}, function(err, channelUpdateResp, response) {
});
更新当前用户频道列表中找到的第一个频道的横幅的完整示例:
youtube = google.youtube({
version: 'v3',
auth: oauth2Client
});
youtube.channelBanners.insert({
media: {
mimeType: "image/jpeg",
body: fs.createReadStream('banner.jpeg')
}
}, function(err, uploadResponse, response) {
if (err)
console.error("channelBanners.insert error : ", err);
if (response)
console.log('channelBanners.insert : ' + response.statusCode);
if (uploadResponse && uploadResponse.url) {
console.log("setting channel brandingSettings : " + uploadResponse.url);
youtube.channels.list({
part: "brandingSettings",
mine: true
}, function(err, channelListRsp, response) {
if (err)
console.error('channels.list error : ', err);
if (response)
console.log('channels.list : ' + response.statusCode);
if (channelListRsp && channelListRsp.items && channelListRsp.items.length > 0) {
console.log("updating banner for channel id : " + channelListRsp.items[0].id);
// set the url
channelListRsp.items[0].brandingSettings.image.bannerExternalUrl = uploadResponse.url;
//update channel brandingSettings
youtube.channels.update({
part: "brandingSettings",
resource: channelListRsp.items[0]
}, function(err, channelUpdateResp, response) {
if (err)
console.error('channels.update error : ', err);
if (response)
console.log('channels.update : ' + response.statusCode);
if (channelUpdateResp)
console.log(channelUpdateResp);
});
}
});
}
});
我正在编写一个上传和更新 youtube 频道横幅的应用程序。我正在使用 node.js 和 google api 客户端。 在官方 api 文档中没有关于 node.js 如何发送图像内容的示例,也没有关于回调签名的信息 这是我的代码:
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2();
var youtube = google.youtube({ version: 'v3' });
//Setting the credentials
oauth2Client.setCredentials({
access_token: 'STORED_IN_DATABASE',
refresh_token: 'STORED_IN_DATABASE',
});
//Send the request
youtube.channelBanners.insert({
auth: oauth2Client,
//image_content
}, callback);
调用insert方法后,还要调用channels.update方法,也没有node.js
上的例子从 Channel banners insert,您必须致电:
channelBanners.insert
,并从响应 中获取url字段
channels.update
与之前检索到的 URL
您还需要 channels.update
请求的频道 ID。还要注意 from Channel update :
If you are submitting an update request, and your request does not specify a value for a property that already has a value, the property's existing value will be deleted.
因此您可能需要调用 channels.list
来获取要更新的 brandingSettings
部分的通道对象
与 google-api-nodejs-client 的 API 通话:
-
youtube.channelBanners.insert({ media: { mimeType: "image/jpeg", body: fs.createReadStream('banner.jpeg') } }, function(err, uploadResponse, response) { });
可用的 mimeType
是 image/jpeg
、image/png
、application/octet-stream
-
youtube.channels.list({ part: "brandingSettings", mine: true }, function(err, channelListRsp, response) { });
-
channelListRsp.items[0].brandingSettings.image.bannerExternalUrl = uploadResponse.url; youtube.channels.update({ part: "brandingSettings", resource: channelListRsp.items[0] }, function(err, channelUpdateResp, response) { });
更新当前用户频道列表中找到的第一个频道的横幅的完整示例:
youtube = google.youtube({
version: 'v3',
auth: oauth2Client
});
youtube.channelBanners.insert({
media: {
mimeType: "image/jpeg",
body: fs.createReadStream('banner.jpeg')
}
}, function(err, uploadResponse, response) {
if (err)
console.error("channelBanners.insert error : ", err);
if (response)
console.log('channelBanners.insert : ' + response.statusCode);
if (uploadResponse && uploadResponse.url) {
console.log("setting channel brandingSettings : " + uploadResponse.url);
youtube.channels.list({
part: "brandingSettings",
mine: true
}, function(err, channelListRsp, response) {
if (err)
console.error('channels.list error : ', err);
if (response)
console.log('channels.list : ' + response.statusCode);
if (channelListRsp && channelListRsp.items && channelListRsp.items.length > 0) {
console.log("updating banner for channel id : " + channelListRsp.items[0].id);
// set the url
channelListRsp.items[0].brandingSettings.image.bannerExternalUrl = uploadResponse.url;
//update channel brandingSettings
youtube.channels.update({
part: "brandingSettings",
resource: channelListRsp.items[0]
}, function(err, channelUpdateResp, response) {
if (err)
console.error('channels.update error : ', err);
if (response)
console.log('channels.update : ' + response.statusCode);
if (channelUpdateResp)
console.log(channelUpdateResp);
});
}
});
}
});