title: Google API 401 Invalid Credentials
body: <p>I'm trying to use <a href="https://developers.google.com/accounts/docs/OAuth2InstalledApp" rel="nofollow">Oauth2</a> to access Analytics data. I successfully get authorization code from and change it for authorization token. But when I try to get any data from Analytics API I get <strong>401 Invalid Credentials</strong> error.</p>
  
  <p>The code I'm using is from <a href="https://code.google.com/p/google-api-php-client/wiki/OAuth2" rel="nofollow">PHP client library</a>:</p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', 1);
  error_reporting(E_ALL);
  
  require_once 'lib/API/Google_Client.php';
  require_once 'lib/API/contrib/Google_AnalyticsService.php';
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("APP name");
  
  $client-&gt;setClientId('xxx.apps.googleusercontent.com');
  $client-&gt;setClientSecret('mysecret');         
  $client-&gt;setRedirectUri('http://example.com');
  $client-&gt;setDeveloperKey('mykey');
  
  $client-&gt;setScopes('https://www.googleapis.com/auth/analytics');
  
  $service = new Google_AnalyticsService($client);
  
  if (isset($_GET['logout'])) {
      session_destroy();
  }
  
  if (isset($_GET['code'])) {
      $client-&gt;authenticate();
      $_SESSION['token'] = $client-&gt;getAccessToken();
      $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  if (isset($_SESSION['token'])) {
      $client-&gt;setAccessToken($_SESSION['token']);
  //    $client-&gt;refreshToken('refresh-token');
      print_r(json_decode($_SESSION['token']));
  
      if ($client-&gt;isAccessTokenExpired()) {
          session_destroy();
          header('Location: http://example.com');
      }
  }
  
  if ($client-&gt;getAccessToken()) {
      $props = $service-&gt;management_webproperties-&gt;listManagementWebproperties("~all");
      print "&lt;h1&gt;Web Properties&lt;/h1&gt;&lt;pre&gt;" . print_r($props, true) . "&lt;/pre&gt;";
  
      $accounts = $service-&gt;management_accounts-&gt;listManagementAccounts();
      print "&lt;h1&gt;Accounts&lt;/h1&gt;&lt;pre&gt;" . print_r($accounts, true) . "&lt;/pre&gt;";
  
      $segments = $service-&gt;management_segments-&gt;listManagementSegments();
      print "&lt;h1&gt;Segments&lt;/h1&gt;&lt;pre&gt;" . print_r($segments, true) . "&lt;/pre&gt;";
  
      $goals = $service-&gt;management_goals-&gt;listManagementGoals("~all", "~all", "~all");
      print "&lt;h1&gt;Segments&lt;/h1&gt;&lt;pre&gt;" . print_r($goals, true) . "&lt;/pre&gt;";
  
      $_SESSION['token'] = $client-&gt;getAccessToken();
  } else {
      $authUrl = $client-&gt;createAuthUrl();
      print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p>I tripplechecked my credentials. My access token looks something like this:</p>
  
  <pre><code>Object
  (
      [access_token] =&gt; ya29.blablabla
      [token_type] =&gt; Bearer
      [expires_in] =&gt; 3600
      [refresh_token] =&gt; 1/Eblablabla
      [created] =&gt; 1413873721
  )
  </code></pre>
  
  <p>When I try to get webproperties from GA account I get 401 error:</p>
  
  <pre><code>    Google_ServiceException Object
  (
      [errors:protected] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [domain] =&gt; global
                      [reason] =&gt; authError
                      [message] =&gt; Invalid Credentials
                      [locationType] =&gt; header
                      [location] =&gt; Authorization
                  )
  
          )
  
      [message:protected] =&gt; Error calling GET https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0: (401) Invalid Credentials
      [string:Exception:private] =&gt; 
      [code:protected] =&gt; 401
      [file:protected] =&gt; ...lib//API/io/Google_REST.php
      [line:protected] =&gt; 66
      [trace:Exception:private] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [file] =&gt; .../lib/API/io/Google_REST.php
                      [line] =&gt; 36
                      [function] =&gt; decodeHttpResponse
                      [class] =&gt; Google_REST
                      [type] =&gt; ::
                      [args] =&gt; Array
                          (
                              [0] =&gt; Google_HttpRequest Object
                                  (
                                      [batchHeaders:Google_HttpRequest:private] =&gt; Array
                                          (
                                              [Content-Type] =&gt; application/http
                                              [Content-Transfer-Encoding] =&gt; binary
                                              [MIME-Version] =&gt; 1.0
                                              [Content-Length] =&gt; 
                                          )
  
                                      [url:protected] =&gt; https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0
                                      [requestMethod:protected] =&gt; GET
                                      [requestHeaders:protected] =&gt; Array
                                          (
                                              [authorization] =&gt; Bearer ya29.xxx
                                          )
  
                                      [postBody:protected] =&gt; 
                                      [userAgent:protected] =&gt; APP name google-api-php-client/0.6.5
                                      [responseHttpCode:protected] =&gt; 401
                                      [responseHeaders:protected] =&gt; Array
                                          (
                                              [vary] =&gt; Origin
  Referer
  X-Origin
                                              [www-authenticate] =&gt; Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token
                                              [content-type] =&gt; application/json; charset=UTF-8
                                              [date] =&gt; Tue, 21 Oct 2014 14:00:46 GMT
                                              [expires] =&gt; Tue, 21 Oct 2014 14:00:46 GMT
                                              [cache-control] =&gt; private, max-age=0
                                              [x-content-type-options] =&gt; nosniff
                                              [x-frame-options] =&gt; SAMEORIGIN
                                              [x-xss-protection] =&gt; 1; mode=block
                                              [server] =&gt; GSE
                                              [alternate-protocol] =&gt; 443:quic,p=0.01
                                              [transfer-encoding] =&gt; chunked
                                          )
  
                                      [responseBody:protected] =&gt; {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
                                      [accessKey] =&gt; 
                                  )
  
                          )
  
                  )
  
              [1] =&gt; Array
                  (
                      [file] =&gt; .../lib/API/service/Google_ServiceResource.php
                      [line] =&gt; 186
                      [function] =&gt; execute
                      [class] =&gt; Google_REST
                      [type] =&gt; ::
                      [args] =&gt; Array
                          (
                              [0] =&gt; Google_HttpRequest Object
                                  (
                                      [batchHeaders:Google_HttpRequest:private] =&gt; Array
                                          (
                                              [Content-Type] =&gt; application/http
                                              [Content-Transfer-Encoding] =&gt; binary
                                              [MIME-Version] =&gt; 1.0
                                              [Content-Length] =&gt; 
                                          )
  
                                      [url:protected] =&gt; https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0
                                      [requestMethod:protected] =&gt; GET
                                      [requestHeaders:protected] =&gt; Array
                                          (
                                              [authorization] =&gt; Bearer ya29.xxx
                                          )
  
                                      [postBody:protected] =&gt; 
                                      [userAgent:protected] =&gt; APP name google-api-php-client/0.6.5
                                      [responseHttpCode:protected] =&gt; 401
                                      [responseHeaders:protected] =&gt; Array
                                          (
                                              [vary] =&gt; Origin
  Referer
  X-Origin
                                              [www-authenticate] =&gt; Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token
                                              [content-type] =&gt; application/json; charset=UTF-8
                                              [date] =&gt; Tue, 21 Oct 2014 14:00:46 GMT
                                              [expires] =&gt; Tue, 21 Oct 2014 14:00:46 GMT
                                              [cache-control] =&gt; private, max-age=0
                                              [x-content-type-options] =&gt; nosniff
                                              [x-frame-options] =&gt; SAMEORIGIN
                                              [x-xss-protection] =&gt; 1; mode=block
                                              [server] =&gt; GSE
                                              [alternate-protocol] =&gt; 443:quic,p=0.01
                                              [transfer-encoding] =&gt; chunked
                                          )
  
                                      [responseBody:protected] =&gt; {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
                                      [accessKey] =&gt; 
                                  )
  
                          )
  
                  )
  
              [2] =&gt; Array
                  (
                      [file] =&gt; .../lib/API/contrib/Google_AnalyticsService.php
                      [line] =&gt; 1010
                      [function] =&gt; __call
                      [class] =&gt; Google_ServiceResource
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; list
                              [1] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [accountId] =&gt; ~all
                                          )
  
                                  )
  
                          )
  
                  )
  
              [3] =&gt; Array
                  (
                      [file] =&gt; .../oatuhCallback.php
                      [line] =&gt; 46
                      [function] =&gt; listManagementWebproperties
                      [class] =&gt; Google_ManagementWebpropertiesServiceResource
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; ~all
                          )
  
                  )
  
          )
  
      [previous:Exception:private] =&gt; 
  )
  </code></pre>
  
  <p>What am I doing wrong?</p>