youtube api获得渠道货币化状态?

use*_*757 7 youtube-api

是否有任何API来检查Youtube频道的货币化状态?也适用于youtube视频.我尝试youtube数据api但没有得到任何其他api知道货币化是开启还是关闭.

Lin*_*oln 1

我知道这个问题很久以前就被问到了,但现在我仍然发现有这个必要。因此,在 YouTube API 文档中进行了大量搜索后,我发现 Google 没有提供此功能,因此我创建了一个小解决方法。

基本上我所做的就是使用指标estimatedRevenue生成分析报告,如果我出现“禁止错误”或403,我会再次尝试将指标更改为没有estimatedRevenue。如果这次获取的报告数据没有错误,则意味着该渠道没有任何收入,因此没有货币化。

我使用 Nodejs 来说明这一点,因为这是我在项目中使用的,但您可以适应任何其他语言。您可能想查看 Google 官方客户端库:

https://developers.google.com/youtube/v3/libraries

代码片段是这样的:

let isMonetized = true;
this.metrics = 'views,comments,likes,dislikes,estimatedMinutesWatched,grossRevenue,estimatedRevenue'

while (true) {

    try {

        const youtubeAnalytics = googleapis.google.youtubeAnalytics({ version: 'v2', auth });

        const response = await youtubeAnalytics.reports
            .query({
                endDate: '2030-12-30',
                ids: 'channel==MINE',
                metrics: this.reportMetrics,
                startDate: '2000-01-01',
            });

        const responseData = response.data;

        const analyticsInfo = {

            channelId,
            channelName: youtubeTokens[channelId].channelName,
            views: responseData.rows[0][0],
            comments: responseData.rows[0][1],
            likes: responseData.rows[0][2],
            dislikes: responseData.rows[0][3],
            estimatedMinutesWatched: responseData.rows[0][4],
            grossRevenue: responseData.rows[0][5] !== undefined
                ? responseData.rows[0][5]
                : 'Not monetized',
            estimatedRevenue: responseData.rows[0][6] !== undefined
                ? responseData.rows[0][6]
                : 'Not monetized',

        };

        return analyticsInfo;

    } catch (error) {

        if (error.code === 403 && isMonetized) {

            console.log('Could not get reports. Trying again without revenue metrics');
            this.reportMetrics = 'views,comments,likes,dislikes,estimatedMinutesWatched';
            isMonetized = false;

        } else {

            console.log(error);

            return false;

        }

    }

}
Run Code Online (Sandbox Code Playgroud)