使用 Youtube API v3 和服务帐户获取频道 ID

GET Channel ID using Youtube API v3 with Service Account

美好的一天,我尝试使用服务帐户获取频道 ID,因为我在项目的其他范围内使用它。但是当我执行请求处理时,它会给我服务帐户信息。我正在使用 php 顺便说一句

非常感谢,如有任何帮助,我们将不胜感激。

p.d。我将真实帐户信息替换为虚拟文本。

当然还有代码:

$credentials_file = $this->key_dir . $this->key_file['title'];

    $client = new \Google_Client();
    $client->setAuthConfig($credentials_file);
    $client->addScope('https://www.googleapis.com/auth/youtube');

    $client->setApplicationName("Youtube Analytics");
    $client->refreshTokenWithAssertion();

    $token = $client->getAccessToken();
    $accessToken = $token['access_token'];
    if (!empty($accessToken)) {

    $service = new \Google_Service_YouTube($client);

    $data = $service->channels->listChannels('id,snippet', array('mine' => 'true'));

    echo "id: " . $data->items[0]->id . "<br>";
    echo "Kind: " . $data->items[0]->kind . "<br>";
    echo "Snippet -> title : " . $data->items[0]->snippet['title'] . "<br>";

    }

据我所知,Youtube API 不支持服务帐户流。如 document 中所述:

The service account flow supports server-to-server interactions that do not access user information. However, the YouTube Data API does not support this flow. Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate a NoLinkedYouTubeAccount error.

同样在 Youtube v3 Google Service Account Access issue, "There are no plans to add support for Service Account authorization to YouTube Data API v3 calls." As stated in the related SO question #21890982 and #21057358 中:您需要自己的帐户才能使用 V3 为服务创建身份验证而无需用户干预。

希望对您有所帮助。