PHP 谷歌日历 API

在项目中设置此功能的初始阶段。我已在 Google 开发者页面启用它并尝试遵循快速入门指南


https://developers.google.com/calendar/quickstart/php


然而,与大多数 Google API 教程一样,这个“快速入门”不起作用。我通过授权页面并获得了在终端中输入的代码,但很长一段时间后我才收到错误


PHP Fatal error:  Uncaught RuntimeException: Unable to read from stream in [site root]/vendor/guzzlehttp/psr7/src/Stream.php


    Stack trace:

#0 [site root]/vendor/guzzlehttp/psr7/src/functions.php(382): GuzzleHttp\Psr7\Stream->read()

    #1 [site root]/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(214): GuzzleHttp\Psr7\copy_to_stream()

    #2 [site root]/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(133): GuzzleHttp\Handler\StreamHandler->drain()

    #3 [site root]/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(50): GuzzleHttp\Handler\StreamHandler->createResponse()

    #4 [site root]/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(66): GuzzleHttp\Handler\StreamHandler->__invoke()

    #5 [site root]/vendor/guzzlehttp/guzzle/src/Middleware.php(29): GuzzleHttp\PrepareBodyMiddleware->__invoke()

    #6 [site root]/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(70): GuzzleHtt in [site root]/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 52

不会创建 token.json 文件,但该目录是可写的。


喵喔喔
浏览 85回答 1
1回答

米脂

怀疑谷歌代码是错误的。更改第 16 行的范围可以使其正确运行。$client->setScopes("https://www.googleapis.com/auth/calendar.readonly");完整代码:<?phprequire __DIR__ . '/vendor/autoload.php';if (php_sapi_name() != 'cli') {&nbsp; &nbsp; throw new Exception('This application must be run on the command line.');}/**&nbsp;* Returns an authorized API client.&nbsp;* @return Google_Client the authorized client object&nbsp;*/function getClient(){&nbsp; &nbsp; $client = new Google_Client();&nbsp; &nbsp; $client->setApplicationName('Google Calendar API PHP Quickstart');&nbsp; &nbsp; //$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);&nbsp; &nbsp; $client->setScopes("https://www.googleapis.com/auth/calendar.readonly");&nbsp; &nbsp; $client->setAuthConfig('credentials.json');&nbsp; &nbsp; $client->setAccessType('offline');&nbsp; &nbsp; $client->setPrompt('select_account consent');&nbsp; &nbsp; // Load previously authorized token from a file, if it exists.&nbsp; &nbsp; // The file token.json stores the user's access and refresh tokens, and is&nbsp; &nbsp; // created automatically when the authorization flow completes for the first&nbsp; &nbsp; // time.&nbsp; &nbsp; $tokenPath = 'token.json';&nbsp; &nbsp; if (file_exists($tokenPath)) {&nbsp; &nbsp; &nbsp; &nbsp; $accessToken = json_decode(file_get_contents($tokenPath), true);&nbsp; &nbsp; &nbsp; &nbsp; $client->setAccessToken($accessToken);&nbsp; &nbsp; }&nbsp; &nbsp; // If there is no previous token or it's expired.&nbsp; &nbsp; if ($client->isAccessTokenExpired()) {&nbsp; &nbsp; &nbsp; &nbsp; // Refresh the token if possible, else fetch a new one.&nbsp; &nbsp; &nbsp; &nbsp; if ($client->getRefreshToken()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Request authorization from the user.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $authUrl = $client->createAuthUrl();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Open the following link in your browser:\n%s\n", $authUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print 'Enter verification code: ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $authCode = trim(fgets(STDIN));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Exchange authorization code for an access token.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $client->setAccessToken($accessToken);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check to see if there was an error.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists('error', $accessToken)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(join(', ', $accessToken));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Save the token to a file.&nbsp; &nbsp; &nbsp; &nbsp; if (!file_exists(dirname($tokenPath))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mkdir(dirname($tokenPath), 0700, true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents($tokenPath, json_encode($client->getAccessToken()));&nbsp; &nbsp; }&nbsp; &nbsp; return $client;}// Get the API client and construct the service object.$client = getClient();$service = new Google_Service_Calendar($client);// Print the next 10 events on the user's calendar.$calendarId = 'primary';$optParams = array(&nbsp; &nbsp; 'maxResults' => 10,&nbsp; &nbsp; 'orderBy' => 'startTime',&nbsp; &nbsp; 'singleEvents' => true,&nbsp; &nbsp; 'timeMin' => date('c'),);$results = $service->events->listEvents($calendarId, $optParams);$events = $results->getItems();if (empty($events)) {&nbsp; &nbsp; print "No upcoming events found.\n";} else {&nbsp; &nbsp; print "Upcoming events:\n";&nbsp; &nbsp; foreach ($events as $event) {&nbsp; &nbsp; &nbsp; &nbsp; $start = $event->start->dateTime;&nbsp; &nbsp; &nbsp; &nbsp; if (empty($start)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start = $event->start->date;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; printf("%s (%s)\n", $event->getSummary(), $start);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP