程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Node.js:使用 YouTube Data API 设置视频标题和描述大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Node.js:使用 YouTube Data API 设置视频标题和描述?

开发过程中遇到Node.js:使用 YouTube Data API 设置视频标题和描述的问题如何解决?下面主要结合日常开发的经验,给出你关于Node.js:使用 YouTube Data API 设置视频标题和描述的解决方法建议,希望对你解决Node.js:使用 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.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 属性指定一个值(即使这两个属性之前已经设置过)。官方规范还说:

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

因此,在调用 snippet.categoryId 之前,您必须调用 Videos.list API 端点以获取 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,requestBody: {
        snippet: {
            title: "Generic title",description: "Generic description",categoryId: prev.categoryId    
        },localizations: {
            "sq": {           
                title: "Translated title",description: "Translated description"
            }
        }
    }
});

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

大佬总结

以上是大佬教程为你收集整理的Node.js:使用 YouTube Data API 设置视频标题和描述全部内容,希望文章能够帮你解决Node.js:使用 YouTube Data API 设置视频标题和描述所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:YouTube