3
votes

I'm trying to access the YouTube Analytics API in PHP using OAUTH2. In my scenario, I first retrieve the channel data, which works fine (even with authenticated segments). Then I parse the channel-id in my Analytics-call. The YouTube Analytics API is activated in my Google Developer Console. I end up with the following error:

An client error occurred: Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3D##MY_CHANNEL_ID##&start-date=2014-01-01&end-date=2014-01-25&metrics=views&dimensions=7DayTotals&sort=day: (403) Insufficient Permission

Here's my code. Please note that I added my channel-id manually to make sure it's absolutely correct. As I said: the authentication works for the YouTube Data API, but it fails to connect to the Analytics-API.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$OAUTH2_CLIENT_ID = '##MY_CLIENT_ID##';
$OAUTH2_CLIENT_SECRET = '##MY_SECRET##';

set_include_path('##PATH_TO_CLIENT_LIBRARY##');
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
require_once 'Google/Service/YouTubeAnalytics.php';

session_start();

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new Google_Service_YouTube($client);

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
    $analytics = new Google_Service_YouTubeAnalytics($client);

    $id = 'channel==##MY_CHANNEL_ID##';
    $start_date = '2014-01-01';
    $end_date = '2014-01-20';
    $optparams = array(
        'dimensions' => '7DayTotals',
        'sort' => 'day',
    );

    $metrics = array(
        'views',
        'estimatedMinutesWatched',
        'averageViewDuration',
        'comments',
        'favoritesAdded',
        'favoritesRemoved',
        'likes',
        'dislikes',
        'shares',
        'subscribersGained',
        'subscribersLost'
    );

    $api_response = $metrics;

    foreach ($metrics as $metric)
    {
        $api = $analytics->reports->query($id, $start_date, $end_date, $metric, $optparams);
        if (isset($api['rows'])) $api_response[$metric] = $api['rows'][0][0];
    }
}

?>

By the way, if anyone has examples of working PHP-scripts using the YouTube Analytics API, I would be very happy if you could share them. I haven't seen a single working example on the entire WWW yet.

1

1 Answers

4
votes

I already found the answer myself. The problem was in the line

$client->setScopes('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly');

Changing this to the line below made the script work:

$client->setScopes(array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly'));