首页 > 解决方案 > Node.js:使用 YouTube 数据 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) {

怎么做?

标签: node.jsyoutubeyoutube-apiyoutube-data-api

解决方案


您必须承认调用Videos.updateAPI 端点必须如下所示:

案例#1:不更新snippet.titlesnippet.description

const res = await youtube.videos.update({
    part: 'id,localizations',
    id: 'VIDEO_ID',
    requestBody: {
        localizations: {
            "sq": {           
                title: "Translated title",
                description: "Translated description"
            }
        }
    }
});

案例#2:同时更新snippet.titlesnippet.description

根据官方规范,在更新视频的snippet属性时,您需要为属性snippet.title和指定一个值snippet.categoryId(即使之前已经设置了这两个属性)。官方规范还说:

如果您正在提交更新请求,并且您的请求没有为已有值的属性指定值,则该属性的现有值将被删除。

因此,您必须在调用之前调用Videos.listAPI 端点,以便获取:snippet.categoryIdVideos.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"
            }
        }
    }
});

另请注意,上面我使用fields请求参数从 API 仅获取实际需要的信息。


推荐阅读