谷歌Gmail API错误与代码



好的,所以我试着玩Gmail API,但我一直运行这个错误。我看了看谷歌支持论坛,什么也没找到。我得到的错误代码如下所示

警告:fgets()期望参数1是资源字符串/home4/ab60883/public_html/email/quickstart.php on line 32

致命错误:未捕获的异常'Google_Auth_Exception'带有消息中"无效代码"/home/ab60883/public_html/email/Google -api-php-client-master/src/Google/Auth/OAuth2.php:89/home4 ab60883/public_html/电子邮件/google-api-php-client-master/src/谷歌/Client.php (128):Google_Auth_OAuth2->authenticate(", false) #1/home4 ab60883/public_html/电子邮件/quickstart.php (35):Google_Client ->验证()# 2/home4/ab60883/public_html/email/quickstart.php(68): getClient() #3{main}被抛出/home4/ab60883/public_html/电子邮件/google-api-php-client-master/src/谷歌/认证/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%sn", $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 %sn", $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("- %sn", $label->getName());
    }
  }

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

STDIN是相当于HTML输入标记的CLI。

在终端运行php quickstart.php

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

相关内容

  • 没有找到相关文章

最新更新