title: YouTube Analytics API in PHP: I keep getting "Insufficient permissions" while authenticated
body: <p>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:</p>
  
  <blockquote>
    <p>An client error occurred: Error calling GET <a href="https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3D##MY_CHANNEL_ID##&amp;start-date=2014-01-01&amp;end-date=2014-01-25&amp;metrics=views&amp;dimensions=7DayTotals&amp;sort=day" rel="nofollow">https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3D##MY_CHANNEL_ID##&amp;start-date=2014-01-01&amp;end-date=2014-01-25&amp;metrics=views&amp;dimensions=7DayTotals&amp;sort=day</a>: (403) Insufficient Permission</p>
  </blockquote>
  
  <p>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.</p>
  
  <pre><code>&lt;?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-&gt;setClientId($OAUTH2_CLIENT_ID);
  $client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);
  $client-&gt;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-&gt;setRedirectUri($redirect);
  
  $youtube = new Google_Service_YouTube($client);
  
  if (isset($_SESSION['token'])) {
    $client-&gt;setAccessToken($_SESSION['token']);
  }
  
  if ($client-&gt;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' =&gt; '7DayTotals',
          'sort' =&gt; 'day',
      );
  
      $metrics = array(
          'views',
          'estimatedMinutesWatched',
          'averageViewDuration',
          'comments',
          'favoritesAdded',
          'favoritesRemoved',
          'likes',
          'dislikes',
          'shares',
          'subscribersGained',
          'subscribersLost'
      );
  
      $api_response = $metrics;
  
      foreach ($metrics as $metric)
      {
          $api = $analytics-&gt;reports-&gt;query($id, $start_date, $end_date, $metric, $optparams);
          if (isset($api['rows'])) $api_response[$metric] = $api['rows'][0][0];
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>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.</p>