Goolishka
Goolishka

Reputation: 250

How to upload and update youtube channel banner using node js googleapis client

I am writing an application that uploads and updates the youtube channel banner. i am using node.js and google api client. In the official api documentation there's no examples on node.js how to send the image content, also there's no information about the callback's signature Here is my code:

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

After calling the insert method, i have to call channels.update method, which also doesn't have the examples on node.js

Upvotes: 1

Views: 693

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45432

From Channel banners insert, you have to call :

  • channelBanners.insert, and get the url field from the response
  • channels.update with the previous retrieved URL

You will also need the channel id for channels.update request. Note also that 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.

So you may want to call channels.list to get the channel object with brandingSettings part to be updated

The API call with google-api-nodejs-client:

  • channelBanners.insert

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

The available mimeType are image/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) {
    
    });
    

Full example which update the banner for the first channel found in the channel list of current user :

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

Upvotes: 1

Related Questions