Node.js:使用 YouTube 数据设置视频标题和描述 API
Node.js: Setting video title and description using YouTube Data API
我想以编程方式(使用 Node.js)为我在 YouTube 上的视频设置标题和说明。我找不到正确的 API 函数。
与 Google API 的连接工作正常。
这是一个命令行应用.....
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" ]
}
}
这是我的代码:
'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;
这段代码给我一个身份验证错误:
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) {
怎么做?
您必须确认必须按如下所示调用 Videos.update
API 端点:
案例 #1:不更新 snippet.title
和 snippet.description
const res = await youtube.videos.update({
part: 'id,localizations',
id: 'VIDEO_ID',
requestBody: {
localizations: {
"sq": {
title: "Translated title",
description: "Translated description"
}
}
}
});
案例 #2:同时更新 snippet.title
和 snippet.description
According to the official spec,在更新视频的 snippet
属性 时,您需要为属性 snippet.title
和 snippet.categoryId
指定一个值(甚至在这两个属性之前已设置的情况下)。官方规范还说:
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.
因此,在调用 Videos.update
:[=29= 之前,您必须调用 Videos.list
API 端点以获得 snippet.categoryId
]
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"
}
}
}
});
另请注意,上面我使用 fields
请求参数从 API 中仅获取实际需要的信息。
我想以编程方式(使用 Node.js)为我在 YouTube 上的视频设置标题和说明。我找不到正确的 API 函数。 与 Google API 的连接工作正常。
这是一个命令行应用.....
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" ]
}
}
这是我的代码:
'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;
这段代码给我一个身份验证错误:
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) {
怎么做?
您必须确认必须按如下所示调用 Videos.update
API 端点:
案例 #1:不更新 snippet.title
和 snippet.description
const res = await youtube.videos.update({
part: 'id,localizations',
id: 'VIDEO_ID',
requestBody: {
localizations: {
"sq": {
title: "Translated title",
description: "Translated description"
}
}
}
});
案例 #2:同时更新 snippet.title
和 snippet.description
According to the official spec,在更新视频的 snippet
属性 时,您需要为属性 snippet.title
和 snippet.categoryId
指定一个值(甚至在这两个属性之前已设置的情况下)。官方规范还说:
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.
因此,在调用 Videos.update
:[=29= 之前,您必须调用 Videos.list
API 端点以获得 snippet.categoryId
]
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"
}
}
}
});
另请注意,上面我使用 fields
请求参数从 API 中仅获取实际需要的信息。