0
votes

Ok so im trying to play with the Gmail API but I keep runnning with this error. I looked at the google support forums and found nothing. The error code that i get is this below

Warning: fgets() expects parameter 1 to be resource, string given in /home4/ab60883/public_html/email/quickstart.php on line 32

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Invalid code' in /home4/ab60883/public_html/email/google-api-php-client-master/src/Google/Auth/OAuth2.php:89 Stack trace: #0 /home4/ab60883/public_html/email/google-api-php-client-master/src/Google/Client.php(128): Google_Auth_OAuth2->authenticate('', false) #1 /home4/ab60883/public_html/email/quickstart.php(35): Google_Client->authenticate('') #2 /home4/ab60883/public_html/email/quickstart.php(68): getClient() #3 {main} thrown in /home4/ab60883/public_html/email/google-api-php-client-master/src/Google/Auth/OAuth2.php on line 89

is there something I'm doing wrong? I'm suppose to get a box to put my verification code but nothing. Any ideas?

  require_once dirname(__FILE__).'/google-api-php-client-master/src/Google/autoload.php';

  define('APPLICATION_NAME', 'Gmail API PHP Quickstart');
  define('CREDENTIALS_PATH', '~/.credentials/gmail-php-quickstart.json');
  define('CLIENT_SECRET_PATH', 'client_secret.json');
  define('SCOPES', implode(' ', array(
    Google_Service_Gmail::GMAIL_READONLY)
  ));

  /**
   * Returns an authorized API client.
   * @return Google_Client the authorized client object
   */
  function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
      $accessToken = file_get_contents($credentialsPath);
    } else {
      // Request authorization from the user.
      $authUrl = $client->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->authenticate($authCode);

      // Store the credentials to disk.
      if(!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
      }
      file_put_contents($credentialsPath, $accessToken);
      printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client->setAccessToken($accessToken);

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
      $client->refreshToken($client->getRefreshToken());
      file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
  }

  /**
   * Expands the home directory alias '~' to the full path.
   * @param string $path the path to expand.
   * @return string the expanded path.
   */
  function expandHomeDirectory($path) {
    $homeDirectory = getenv('HOME');
    if (empty($homeDirectory)) {
      $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
    }
    return str_replace('~', realpath($homeDirectory), $path);
  }

  // Get the API client and construct the service object.
  $client = getClient();
  $service = new Google_Service_Gmail($client);

  // Print the labels in the user's account.
  $user = 'me';
  $results = $service->users_labels->listUsersLabels($user);

  if (count($results->getLabels()) == 0) {
    print "No labels found.\n";
  } else {
    print "Labels:\n";
    foreach ($results->getLabels() as $label) {
      printf("- %s\n", $label->getName());
    }
  }
2

2 Answers

0
votes

Make sure you are running the script from the command line and not your browser.

STDIN is the CLI equivalent of an HTML input tag.

0
votes

Run php quickstart.php in the terminal.

If for some reason it can't find the file, make sure you put it in the root of your project.