title: PHP - Google Admin SDK - Authentication with Service Accounts "Not Authorized to access this resource/api"
body: <p>I'm using the library google/google-api-php-client and I'm creating an authentication with service accounts.</p>
  
  <p>I have created the service account in <a href="https://console.developers.google.com/" rel="nofollow">https://console.developers.google.com/</a> and I added the domain-wide authority to my service account.</p>
  
  <p>But i return i request the following:</p>
  
  <pre><code>Google_Service_Exception in REST.php line 118:
  {
  "error": {
  "errors": [
  {
  "domain": "global",
  "reason": "forbidden",
  "message": "Not Authorized to access this resource/api"
  }
  ],
  "code": 403,
  "message": "Not Authorized to access this resource/api"
  }
  }
  </code></pre>
  
  <p>My function:</p>
  
  <pre><code>$client = new \Google_Client();
  
  $credentials_file = base_path() . '/service-account.json';
  
  if ($credentials_file = $this-&gt;checkServiceAccountCredentialsFile($credentials_file)) {
      $client-&gt;setAuthConfig($credentials_file);
  }
  
  $client-&gt;setApplicationName("app-name");
  $client-&gt;setScopes([
      'https://www.googleapis.com/auth/admin.directory.user',
      'https://www.googleapis.com/auth/admin.directory.user.readonly'
  ]);
  
  $service = new \Google_Service_Directory($client);
  
  $userKey = 'user@domain.com';
  
  $results = $service-&gt;users-&gt;get($userKey);
  
  var_dump($results);
  </code></pre>
  
  <p>If I try here is working.
  <a href="https://developers.google.com/admin-sdk/directory/v1/reference/users/get#auth" rel="nofollow">https://developers.google.com/admin-sdk/directory/v1/reference/users/get#auth</a></p>