Reputation: 185
I'll like to programatically (with Node.js) set the title and description for my videos on YouTube. I cannot find the correct API function. The connection to Google API works fine.
It's a Command line app .....
oauth2keys.json:
{
"installed":
{
"client_id":"b.apps.googleusercontent.com",
"project_id":"name-app",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"McfS",
"redirect_uris": [ "http://localhost:3000/oauth2callback" ]
}
}
Here is my code:
'use strict';
const {google} = require('googleapis');
const path = require('path');
const {authenticate} = require('@google-cloud/local-auth');
// initialize the Youtube API library
const youtube = google.youtube('v3');
// a very simple example of searching for youtube videos
async function runSample() {
const auth = await authenticate({
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
scopes: ['https://www.googleapis.com/auth/youtube'],
});
google.options({auth});
const res0 = await youtube.videos.list({
fields: 'items/snippet/categoryId',
part: 'snippet',
id: 'VIDEO_ID'
});
const prev = res0.data.items[0].snippet;
const res = await youtube.videos.update({
part: 'id,snippet,localizations',
id: 'VIDEO_ID',
requestBody: {
snippet: {
title: "Generic title",
description: "Generic description",
categoryId: prev.categoryId
},
localizations: {
"sq": {
title: "Translated title",
description: "Translated description"
}
}
}
});
console.log("RESULT DATA: " + res.data);
}
if (module === require.main) {
runSample().catch(console.error);
}
module.exports = runSample;
This code gives me an authentication error:
GaxiosError: Forbidden
at Gaxios._request (C:\MyData\Youtube\API\youtube_node\node_modules\gaxios\build\src\gaxios.js:112:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async OAuth2Client.requestAsync (C:\MyData\Youtube\API\youtube_node\node_modules\google-auth-library\build\src\auth\oauth2client.js:343:18)
at async runSample (C:\MyData\Youtube\API\youtube_node\sample_translations.js:48:15) {
How to do it?
Upvotes: 0
Views: 1450
Reputation: 6975
You have to acknowledge that calling the Videos.update
API endpoint has to be done as shown below:
Case #1: Not updating snippet.title
and snippet.description
const res = await youtube.videos.update({
part: 'id,localizations',
id: 'VIDEO_ID',
requestBody: {
localizations: {
"sq": {
title: "Translated title",
description: "Translated description"
}
}
}
});
Case #2: Updating both snippet.title
and snippet.description
According to the official spec, while updating a video's snippet
property, you're required to specify a value for the properties snippet.title
and snippet.categoryId
(even in the case these two properties have previously been set). The official spec also says that:
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.
Therefore, you'll have to have a call to Videos.list
API endpoint, for to obtain snippet.categoryId
, prior to invoking Videos.update
:
const res0 = await youtube.videos.list({
fields: 'items/snippet/categoryId',
part: 'snippet',
id: 'VIDEO_ID'
});
const prev = res0.data.items[0].snippet;
const res = await youtube.videos.update({
part: 'id,snippet,localizations',
id: 'VIDEO_ID',
requestBody: {
snippet: {
title: "Generic title",
description: "Generic description",
categoryId: prev.categoryId
},
localizations: {
"sq": {
title: "Translated title",
description: "Translated description"
}
}
}
});
Note also that above I used the fields
request parameter for to get from the API only the info that's actually needed.
Upvotes: 3