title: How to fetch auth token for service account using google API?
body: <p>I have a Wordpress site (running PHP) that needs to display its own analytics data to visitors using the google-api. I want to create the charts in Javascript, so I need to fetch an auth token from the PHP code and then pass that to the Javascript code. I can't figure out how to get the auth token.</p>
  
  <p>So far, I have code similar to this working using my service account:
  <a href="https://github.com/google/google-api-php-client/blob/master/examples/service-account.php" rel="nofollow">https://github.com/google/google-api-php-client/blob/master/examples/service-account.php</a></p>
  
  <p>My code:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setAuthConfig($credentialsFilePath);
  
  $client-&gt;addScope('https://www.googleapis.com/auth/analytics.readonly');
  $client-&gt;setApplicationName("GoogleAnalytics");
  
  
  $analytics = new Google_Service_Analytics($client);
  $ga = $analytics-&gt;data_ga;
  
  $start = date('Y-m-d', strtotime('-7 days'));
  $end   = date('Y-m-d');
  
  $views = $ga-&gt;get('ga:'.$myAnalyticsId,
                  $start,
                  $end,
                  'ga:pageviews,ga:sessions,ga:newUsers',
                  array(
                      'dimensions' =&gt; 'ga:date',
                      'sort' =&gt; 'ga:date'
                  ));
  </code></pre>
  
  <p>This all works, and I'm able to connect to Google_Service_Analytics and fetch analytics data. However, I can't figure out how to fetch a service access token using my credentials, that I can hand off to the Javascript code so I can use the Google Analytics API from Javascript.</p>
  
  <p>This doesn't work:</p>
  
  <pre><code>$token = $client-&gt;getAccessToken();
  </code></pre>
  
  <p>Token just ends up being null. What do I need to do to fetch a token?</p>