Google日历不起作用



Google日历无法正常工作,获取错误" 致命错误:e extrawork extrawork wamp www www alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha alpha www alpha alpha www alpha alpha alpha alpha alpha alpha alpha alpha alpha 应用程序模块 MyAccount Controllers Google-Api Src google google client.php "


有人可以帮我,谢谢吗?

    public function google_api(){
        error_reporting(E_ALL);
        require_once('google-api/vendor/autoload.php');

        define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
        define('CREDENTIALS_PATH', '~/credential/credential.json');
        define('CLIENT_SECRET_PATH', __DIR__ . 'google-apiclient_secret.json');
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/calendar-php-quickstart.json
        define('SCOPES', implode(' ', array(
          Google_Service_Calendar::CALENDAR_READONLY)
        ));
        // Get the API client and construct the service object.
        $client = $this->getClient();
        $service = new Google_Service_Calendar($client);
        // Print the next 10 events on the user's calendar.
        $calendarId = 'primary';
        $optParams = array(
          'maxResults' => 10,
          'orderBy' => 'startTime',
          'singleEvents' => TRUE,
          'timeMin' => date('c'),
        );
        $results = $service->events->listEvents($calendarId, $optParams);
        if (count($results->getItems()) == 0) {
          print "No upcoming events found.n";
        } else {
          print "Upcoming events:n";
          foreach ($results->getItems() as $event) {
            $start = $event->start->dateTime;
            if (empty($start)) {
              $start = $event->start->date;
            }
            printf("%s (%s)n", $event->getSummary(), $start);
          }
        }
        if (php_sapi_name() != 'cli') {
          throw new Exception('This application must be run on the command line.');
        }
    }
    // Code Start
    /**
     * 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->setAuthConfig(CLIENT_SECRET_PATH);
      $client->setAccessType('offline');
      // Load previously authorized credentials from a file.
      $credentialsPath = $this->expandHomeDirectory(CREDENTIALS_PATH);
      if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
      } 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->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 %sn", $credentialsPath);
      }
      $client->setAccessToken($accessToken);
      // Refresh the token if it's expired.
      if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($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);
    }
    // Code End

`

您可以使用此相关的GitHub问题参考,这可能是API抛出'无效代码'例外的原因,是访问令牌的格式与在中生成的格式不匹配新的API。

此代码也可能会有所帮助:

  • 获得内容时,我使用了序列化函数

    • 示例:$accessToken = serialize (file_get_contents($credentialsPath));
  • 插入内容时,我使用了delelialize函数

    • 示例:$accessToken = derialize(file_put_contents($credentialsPath, $accessToken););

尝试在所有读取$accessToken$accessTokenunserialize()的地方插入serialize()

问题是凭据。我在网络浏览器中得到一个字符串,如下 https://accounts.google.com/o/oauth2/auth?Response_type = code&Access_type = offline& pclient_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&amp.amp. amp.pop; amp.pop. amp. amp.pop;%2fauth%2fcalendar.Readonly& Apploval_prompt = auto 当我在Web浏览器中打开此URL时,我得到了代码。我已经复制了该代码,并用$ authCode = trim(code_i_got)替换了$ authcode = trim(fgets(stdin))。此过程已经生成了凭据。json文件,之后我能够成功运行我的代码

最新更新