gpt4 book ai didi

javascript - 如何使用 node js googleapis 客户端上传和更新 youtube channel 横幅

转载 作者:行者123 更新时间:2023-11-30 21:20:39 25 4
gpt4 key购买 nike

我正在编写一个上传和更新 youtube channel 横幅的应用程序。我正在使用 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 请求的 channel 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 部分的 channel 对象

API 调用 google-api-nodejs-client :

  • channelBanners.insert

    youtube.channelBanners.insert({
    media: {
    mimeType: "image/jpeg",
    body: fs.createReadStream('banner.jpeg')
    }
    }, function(err, uploadResponse, response) {

    });

可用的mimeTypeimage/jpeg, image/png, application/octet-stream

  • channels.list

    youtube.channels.list({
    part: "brandingSettings",
    mine: true
    }, function(err, channelListRsp, response) {

    });
  • channels.update

    channelListRsp.items[0].brandingSettings.image.bannerExternalUrl = uploadResponse.url;

    youtube.channels.update({
    part: "brandingSettings",
    resource: channelListRsp.items[0]
    }, function(err, channelUpdateResp, response) {

    });

为当前用户 channel 列表中找到的第一个 channel 更新横幅的完整示例:

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);
});
}
});
}
});

关于javascript - 如何使用 node js googleapis 客户端上传和更新 youtube channel 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45213754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com