【问题标题】:YouTube Reporting API using Service Account使用服务帐户的 YouTube 报告 API
【发布时间】:2026-01-22 17:15:01
【问题描述】:

按照 Google 的文档,您应该能够通过使用服务帐户在无需用户身份验证的情况下使用 YouTube Reporting API。

目前我有一个非常简单的示例,它应该给我一个可用工作列表,但可能是空的。但我得到了 403 权限被拒绝。

请求

$client = new \Google_Client();
$client->setAuthConfig('service-account-key.json');

$client->addScope(Google_Service_YouTubeReporting::YT_ANALYTICS_MONETARY_READONLY);
$client->addScope(Google_Service_YouTubeReporting::YT_ANALYTICS_READONLY);

$youtube_reporting_service = new \Google_Service_YouTubeReporting($client);

$jobs = $youtube_reporting_service->jobs->listJobs();

回应

Google_Service_Exception

{
    "error": {
        "code": 403,
        "message": "The caller does not have permission",
        "errors": [
            {
                "message": "The caller does not have permission",
                "domain": "global",
                "reason": "forbidden"
            }
        ],
        "status": "PERMISSION_DENIED"
    }
}

我验证了以下 API 已启用:

  • YouTube 报告 API
  • YouTube 数据 API v3
  • YouTube 分析 API
  • Google+ API (如果真的有必要,我对此表示怀疑)

我不明白的是 Google Cloud Platform 中的项目与 YouTube 帐户之间的关系应该如何。

这和Delegating domain-wide authority to the service account有关系吗?

您是否需要启用 G Suite?

我看到一个类似的问题How to grant access to Youtube Reporting API for a google service account? 存在,但自上次回复以来,这个问题可能有点老了。我希望我的示例和补充内容能为这个问题带来新的启发。

有人有工作样本吗?可能是其他语言。只要流程畅通。

【问题讨论】:

    标签: php youtube youtube-api gcp google-client-login


    【解决方案1】:

    一般来说,服务帐号可用于模拟用户帐号。 Preparing to make an authorized API call 中提到了进行授权调用。

    但要回答这个问题,Service Accounts do not work with the YouTube API

    服务帐户不适用于 YouTube 数据 API 调用,因为 服务帐户需要关联的 YouTube 频道,而您不能 将新的或现有的频道与服务帐户相关联。如果你使用一个 服务帐户调用 YouTube 数据 API,API 服务器返回 错误类型设置为未授权且原因设置为的错误 youtubeSignupRequired。

    【讨论】:

      最近更新 更多