LoginSignup
1
1

Node.jsでYouTubeにアップロード済みの動画のタイトルや概要を更新する - GPTに最初嘘つかれた

Last updated at Posted at 2023-12-22

この辺りの記事の続きです。

videos.updateでいけそう

こんな感じでいけそうでした。

const res = await youtube.videos.update({
    part: 'snippet',
    requestBody: {
        id: videoId,
        snippet: {
            title: newTitle,
            description: newDescription,
            categoryId: 27 //カテゴリID
        }
    }
});

動いたコード

'use strict';

const fs = require('fs');
const {google} = require('googleapis');

const googleAuth = () => {
    const CREDENTIALS_PATH = 'client_secret.json';
    const TOKEN_PATH = 'token.json';
    const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_PATH, 'utf8'));
    const token = JSON.parse(fs.readFileSync(TOKEN_PATH, 'utf8'));

    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
    oAuth2Client.setCredentials(token);

    return oAuth2Client;
};

const videoId = `XXXXXX`;
const newTitle = 'たいとるへんこう';
const newDescription = 'がいようもへんこうしてみたよ';
const categoryId = 27;

(async () => {
    const auth = googleAuth();
    const youtube = google.youtube({version: 'v3', auth});

    try {
        
        const res = await youtube.videos.update({
            part: 'snippet',
            requestBody: {
                id: videoId,
                snippet: {
                    title: newTitle,
                    description: newDescription,
                    categoryId: categoryId
                }
            }
        });

        console.log(res.data); 

    } catch (error) {
        console.log('The API returned an error: ' + error);
    }
})();

実行すると無事に変更できました。

スクリーンショット 2023-12-23 1.05.38.png

27ってなんだって感じですよね ↓に続く

最初でたエラー - snippet.categoryId property specifies an invalid category ID

というエラーが出ました。

The <code>snippet.categoryId</code> property specifies an invalid category ID. Use the <code><a href="/youtube/v3/docs/videoCategories/list">videoCategories.list</a></code> method to retrieve supported categories.

カテゴリーIDの指定が必要っぽく、指定なしてで試してました。

カテゴリーIDってなんぞって感じですが...

カテゴリーIDの取得

どうやらYouTube動画のジャンルのようなものぽいです。
国ごとで使えるカテゴリーIDが違うみたいでAPI経由で調べられました。

(async () => {
    const auth = googleAuth();
    const youtube = google.youtube({version: 'v3', auth});

    const response = await youtube.videoCategories.list({
        part: 'snippet',
        regionCode: 'JP' // 例: 'US'
    });

    const ids = response.data.items.map(category => ({
        id: category.id,
        title: category.snippet.title
    }));
    console.log(ids);
})();

JP指定で実行したら以下のような情報が出てきます。

[
  { id: '1', title: 'Film & Animation' },
  { id: '2', title: 'Autos & Vehicles' },
  { id: '10', title: 'Music' },
  { id: '15', title: 'Pets & Animals' },
  { id: '17', title: 'Sports' },
  { id: '18', title: 'Short Movies' },
  { id: '19', title: 'Travel & Events' },
  { id: '20', title: 'Gaming' },
  { id: '21', title: 'Videoblogging' },
  { id: '22', title: 'People & Blogs' },
  { id: '23', title: 'Comedy' },
  { id: '24', title: 'Entertainment' },
  { id: '25', title: 'News & Politics' },
  { id: '26', title: 'Howto & Style' },
  { id: '27', title: 'Education' },
  { id: '28', title: 'Science & Technology' },
  { id: '30', title: 'Movies' },
  { id: '31', title: 'Anime/Animation' },
  { id: '32', title: 'Action/Adventure' },
  { id: '33', title: 'Classics' },
  { id: '34', title: 'Comedy' },
  { id: '35', title: 'Documentary' },
  { id: '36', title: 'Drama' },
  { id: '37', title: 'Family' },
  { id: '38', title: 'Foreign' },
  { id: '39', title: 'Horror' },
  { id: '40', title: 'Sci-Fi/Fantasy' },
  { id: '41', title: 'Thriller' },
  { id: '42', title: 'Shorts' },
  { id: '43', title: 'Shows' },
  { id: '44', title: 'Trailers' }
]
  • Short Movies (ID 18)
  • Education(ID 27)
  • Science & Technology (ID 28)

あたりが個人的によく使いそうです。

ということで27を指定して試してみたのが先ほどのコードです。

よもやま。GPTが始め嘘ついてきた

これなかなかそれっぽいですよね。

YouTube Data APIを使って特定の動画のタイトルを変更することは、APIの制約により直接はできません。YouTube APIを使用して動画のタイトルを変更するには、動画の所有者がYouTube Studioを介して手動で行う必要があります。

スクリーンショット 2023-12-23 1.00.51.png

こんな感じでドキュメントを調べてから聞いてみたら手のひらを返してきました。

スクリーンショット 2023-12-23 1.02.10.png

パッと見よさそうな回答はしてくれますが、怪しそうな時は自分で調べるのもまだ大事なだと実感

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1