Search code examples
youtube-apiyoutube-data-apiyoutube-analyticsyoutube-analytics-api

GET Channel ID using Youtube API v3 with Service Account


Good Day, I Try to Get Channel ID using service account, because I´m using it on other scopes on my project. But when I do request process its give me the service account information. i´m using php btw

Many thanks in advance and any help will be appreciated.

p.d. i´m replace real account info for dummy text.

Channel id on the tab and results image

And of Course the code :

$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>";

    }

Solution

  • AFAIK, Youtube API doesn't support Service Account Flow. As described in the 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.

    Also in the 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 : you'll need your own account for that and you can create an authentication for services using V3 without user intervention.

    Hope this helps.