title: How to create gmail business user account using google API with PHP
body: <p>I want to create a new <strong>gmail business user account</strong> in gmail using PHP.  When I run this code it will shows some message like <code>Open the following link in your browser:</code> with URL and asking for verification code. If I run the link in browser it will generate some random string. I got <strong>403 error</strong> when I paste the random string in terminal for verification. Here is my code.</p>
  
  <pre><code>&lt;?php
  require_once __DIR__ . '/vendor/autoload.php';
  
  
  define('APPLICATION_NAME', 'Directory API PHP Quickstart');
  define('CREDENTIALS_PATH', '~/.credentials/admin-directory_v1-php-quickstart.json');
  define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
  // If modifying these scopes, delete your previously saved credentials
  // at ~/.credentials/admin-directory_v1-php-quickstart.json
  define('SCOPES', implode(' ', array(
    Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY)
  ));
  
  if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
  }
  
  /**
   * Returns an authorized API client.
   * @return Google_Client the authorized client object
   */
  function getClient() {
    $client = new Google_Client();
    $client-&gt;setApplicationName(APPLICATION_NAME);
    $client-&gt;setScopes(SCOPES);
    $client-&gt;setAuthConfig(CLIENT_SECRET_PATH);
    $client-&gt;setAccessType('offline');
  
    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
      $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } else {
      // Request authorization from the user.
      $authUrl = $client-&gt;createAuthUrl();
      printf("Open the following link in your browser:\n%s\n", $authUrl);
      print 'Enter verification code: ';
      $authCode = trim(fgets(STDIN));
  
      // Exchange authorization code for an access token.
      $accessToken = $client-&gt;fetchAccessTokenWithAuthCode($authCode);
  
      // Store the credentials to disk.
      if(!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
      }
      file_put_contents($credentialsPath, json_encode($accessToken));
      printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client-&gt;setAccessToken($accessToken);
  
    // Refresh the token if it's expired.
    if ($client-&gt;isAccessTokenExpired()) {
      $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
      file_put_contents($credentialsPath, json_encode($client-&gt;getAccessToken()));
    }
    return $client;
  }
  </code></pre>
  
  <p>My error </p>
  
  <blockquote>
    <p>Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "forbidden", "message": "Not Authorized to access this resource/api" } ], "code": 403, "message": "Not Authorized to access this resource/api" } }</p>
  </blockquote>