代码为

时间:2015-10-25 04:29:26

标签: php google-api gmail google-api-php-client gmail-api

好的,所以即时尝试使用Gmail API,但我一直在运行此错误。我看了谷歌支持论坛,一无所获。我得到的错误代码如下所示

  

警告:fgets()期望参数1为资源,字符串为   第32行/home4/ab60883/public_html/email/quickstart.php

     

致命错误:带有消息的未捕获异常'Google_Auth_Exception'   '无效代码'   /home4/ab60883/public_html/email/google-api-php-client-master/src/Google/Auth/OAuth2.php:89堆栈追踪:#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}   /home4/ab60883/public_html/email/google-api-php-client-master/src/Google/Auth/OAuth2.php   第89行

有什么我做错了吗?我想要一个盒子来放置我的验证码,但没有。有什么想法吗?

  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 个答案:

答案 0 :(得分:0)

确保从命令行而不是浏览器运行脚本。

STDIN是与HTML输入标记等效的CLI。

答案 1 :(得分:0)

在终端中运行php quickstart.php

如果由于某种原因无法找到该文件,请确保将其放在项目的根目录中。