Google日历无法正常工作

米图尔·帕雷克(Mitul Parekh)

Google日历无法正常运行,出现错误“致命错误:E:\ Extrawork \ wamp \ www \ alpha \ application \ modules \ myaccount \ controllers \ google-api \ src \中的消息为'Invalid code'的未捕获异常'InvalidArgumentException' 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-api\client_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%s\n", $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 %s\n", $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

`

米图尔·帕雷克(Mitul Parekh)

问题是之前未生成凭证文件夹中的credential.json文件。我在网络浏览器中收到如下字符串:https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=XXXXXXX&redirect_uri=XXXXX&state&scope=https%3A%2F%2F%2Fwww.googleapis.com%2Fauth %2Fcalendar.readonly&approval_prompt = auto在Web浏览器中打开此URL时,我得到了一个代码。我已经复制了该代码,并用$ authCode = trim(CODE_I_GOT)替换了$ authCode = trim(fgets(STDIN))。此过程已生成credential.json文件,此后我便能够成功运行我的代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

日历无法正常工作

来自分类Dev

Java日历无法正常工作

来自分类Dev

Java日历无法正常工作

来自分类Dev

ASP日历无法正常工作

来自分类Dev

Python中的日历模块无法正常工作

来自分类Dev

Google Earth无法正常工作

来自分类Dev

无法使Google教程正常工作

来自分类Dev

Google字体无法正常工作

来自分类Dev

Google Earth无法正常工作

来自分类Dev

fullcalender无法正常工作Uncaught ReferenceError:日历不是构造函数

来自分类Dev

Excel自定义日历功能无法正常工作

来自分类Dev

Datepicker隐藏日历以显示月份和整个日历无法正常工作

来自分类Dev

Google Charts interpolateNull无法正常工作

来自分类Dev

无法使Google Cloud Messaging正常工作

来自分类Dev

Google Maps LatLngbounds无法正常工作

来自分类Dev

无法使Google-api正常工作

来自分类Dev

Google Map Tilt无法正常工作

来自分类Dev

Google+登录无法正常工作

来自分类Dev

迅捷的Google地图代表无法正常工作

来自分类Dev

Android Google地图无法正常工作

来自分类Dev

Google Chrome标签搜索无法正常工作

来自分类Dev

Google Maps API标记无法正常工作

来自分类Dev

Google Apps脚本:jQuery无法正常工作

来自分类Dev

Google Map Circle无法正常工作

来自分类Dev

Google+登录无法正常工作

来自分类Dev

无法使Google Maps Geocode正常工作

来自分类Dev

Android Google地图无法正常工作

来自分类Dev

包含的Google Webfont无法正常工作

来自分类Dev

SignedApk后Google登录无法正常工作