Youtube 分析 &Google 服务帐号 [英] Youtube Analytics & Google Service Account

查看:24
本文介绍了Youtube 分析 &Google 服务帐号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:实现一个程序(java 或 python)从我在我的 Youtube 频道上发布的视频中检索数据.此计划将每天(凌晨 1:00)启动.

Objectives: Implement a program (java or python) to retrieve data from videos that I published on my Youtube channel. This program will be launched daily (1:00 AM).

解决方案:要检索 Youtube 数据,包括每天的观看次数,YouTube Analytics API 在我看来是最好的解决方案.我使用 Google 帐户服务(GoogleCredential")来验证我的身份:

Solutions: To retrieve data Youtube, including the number of views per day, YouTube Analytics API is in my opinion the best solution. I use the Google Account Service ("GoogleCredential") to authenticate me:

static {
    // Build service account credential.
    try {

        // Create a listener for automatic refresh OAuthAccessToken
        List<CredentialRefreshListener> list = new ArrayList<CredentialRefreshListener>();
        list.add(new CredentialRefreshListener() {

            public void onTokenResponse(Credential credential,
                    TokenResponse tokenResponse) throws IOException {
                System.out.println(tokenResponse.toPrettyString());

            }

            public void onTokenErrorResponse(Credential credential,
                    TokenErrorResponse tokenErrorResponse)
                    throws IOException {
                System.err.println("Error: "
                        + tokenErrorResponse.toPrettyString());
            }
        });

        // Create a GoogleCredential for authenticate with ServiceAccount
        // service
        credential = new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(SCOPES)
                .setClock(Clock.SYSTEM)
                .setServiceAccountPrivateKeyFromP12File(
                        new File("key.p12"))
                .setRefreshListeners(list).build();

        credential.refreshToken();

    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后我执行 Youtube Analytics 查询:

And I execute Youtube Analytics query:

YoutubeAnalytics youtubeAnalytics = new YoutubeAnalytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName("Test-YouTube-Analytics/1.0").build();

    // Create request
    credential.refreshToken();
    YoutubeAnalyticsRequest<?> request = youtubeAnalytics.reports()
            .query("channel==" + channelId, "2012-10-01", "2012-12-01", "views")
            .setAlt("json")
            .setKey(API_KEY)
            .setDimensions("month")
            .setPrettyPrint(true);
    System.out.println(request.buildHttpRequest().getUrl().toString());
    ResultTable first = (ResultTable) request.execute();
}

但我收到以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 500 Internal Server Error
{
  "code" : 500,
  "errors" : [ {
    "domain" : "global",
    "message" : "Unknown error occurred on the server.",
    "reason" : "internalError"
  } ],
  "message" : "Unknown error occurred on the server."
}

感谢您的洞察力!

推荐答案

在发出 YouTube 分析 API 请求时,您不能使用服务帐户.您需要使用 YouTube 频道所有者或与该频道关联的内容所有者的帐户,我认为服务帐户不能是其中任何一个.请在以拥有 YouTube 频道的 Google 帐户登录时完成一次 OAuth 2 流程,然后可以在将来重复使用保存的 OAuth 2 刷新令牌以获取可用于运行报告的新访问令牌.

You can't use a service account when making a YouTube Analytics API request. You need to use an account that is either the owner of the YouTube channel or a content owner associated with the channel, and I don't believe a service account can be either of those things. Please go through the OAuth 2 flow once while signed in as the Google Account that owns the YouTube channel, and the saved OAuth 2 refresh token could then be used repeatedly in the future to get fresh access tokens which can be used to run reports.

能否请您解决该问题,然后再次尝试运行您的报告?

Could you please resolve that issue and then try running your report again?

这篇关于Youtube 分析 &amp;Google 服务帐号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆