【问题标题】:Node.js: Setting video title and description using YouTube Data APINode.js:使用 YouTube 数据 API 设置视频标题和描述
【发布时间】:2021-05-28 04:05:20
【问题描述】:

我想以编程方式(使用 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) {

怎么做?

【问题讨论】:

  • 认为您正在寻找 youtube.videos.update
  • 谢谢.... ow 说:GaxiosError: Forbidden
  • 可能是因为您的 Auth 部分丢失了。
  • oauth2.keys.json 在那里,从谷歌云控制台下载

标签: node.js youtube youtube-api youtube-data-api


【解决方案1】:

您必须确认调用Videos.update API 端点必须按如下所示进行:

案例 #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

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

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

因此,在调用Videos.update 之前,您必须调用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 中仅获取实际需要的信息。

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @Marco Martin:请关注我在上述聊天中的帖子。他们将我们的评论线程全部移到那里。
  • @Marco Martin:确实你是对的:即使对于桌面应用程序,Google 开发者控制台也需要验证他/她的应用程序,因为仅使用无处不在的范围 @ 987654341@。这是非常新的行为;我自己和got it confirmed 测试了它是事实。
  • @Marco Martin:Google 只促进较旧的项目(那些在向 API 用户强加此新要求之前创建的项目)将 Publishing status 设置为 in production;这允许较旧的凭据数据(即刷新令牌)——在引入新规则之前创建的那些——在 7 天后不会过期。
猜你喜欢
  • 2016-03-05
  • 2014-02-09
  • 1970-01-01
  • 2012-05-27
  • 2012-02-08
  • 2019-03-30
  • 1970-01-01
  • 2019-10-17
  • 2012-05-20
相关资源
最近更新 更多