簡體   English   中英

403 Google日歷的禁止錯誤

[英]403 Forbidden error with Google Calendar

我一直在嘗試使用Calendar API訪問我的Google日歷。 在我的開發機上,我可以訪問數據並在日歷上顯示事件,但是當我將其推送到生產服務器時,在授予對日歷的訪問權限后會出現以下錯誤。

禁止的

您無權訪問此服務器上的/secondavechurch/calendar2.php。

此外,在嘗試使用ErrorDocument處理請求時遇到403禁止錯誤。

我注冊了一個服務帳戶,並授予訪問日歷的權限,以查看是否是問題所在,但仍然出現相同的錯誤。

這是我的代碼

require_once __DIR__ . '/vendor/autoload.php';

  date_default_timezone_set('America/New_York');

  $REDIRECT_URI = 'http://' . $_SERVER['HTTP_HOST'] .'/calendar2.php';
  $KEY_LOCATION = __DIR__ . '/client_secret.json';
  $SERVICE_ACCOUNT_NAME = 'xxxxxx.iam.gserviceaccount.com';
  $TOKEN_FILE   = "token.txt";

  $SCOPES = array(
      Google_Service_Calendar::CALENDAR_READONLY
  );

  $client = new Google_Client();
  $client->setApplicationName("Second Ave Church Calendar");
  $client->setAuthConfig($KEY_LOCATION);
  $service = new Google_Service_Calendar($client);
  // Incremental authorization
  $client->setIncludeGrantedScopes(true);

  // Allow access to Google API when the user is not present. 
  $client->setAccessType('offline');
  $client->setApprovalPrompt ("force");
  $client->setRedirectUri($REDIRECT_URI);
  $client->setScopes($SCOPES);

  if (isset($_GET['code']) && !empty($_GET['code'])) {
      try {
          // Exchange the one-time authorization code for an access token
          $accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);

          // Save the access token and refresh token in local filesystem
          file_put_contents($TOKEN_FILE, json_encode($accessToken));

          $_SESSION['accessToken'] = $accessToken;
          header('Location: ' . filter_var($REDIRECT_URI, FILTER_SANITIZE_URL));
          exit();
      }
      catch (\Google_Service_Exception $e) {
          print_r($e);
      }
  }

  if (!isset($_SESSION['accessToken'])) {

      $token = @file_get_contents($TOKEN_FILE);

      if ($token == null) {

          // Generate a URL to request access from Google's OAuth 2.0 server:
          $authUrl = $client->createAuthUrl();

          // Redirect the user to Google's OAuth server
          header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
          exit();

      } else {

          $_SESSION['accessToken'] = json_decode($token, true);

      }
  }

  $client->setAccessToken($_SESSION['accessToken']);

  /* Refresh token when expired */
  if ($client->isAccessTokenExpired()) {

      // the new access token comes with a refresh token as well
      $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
      file_put_contents($TOKEN_FILE, json_encode($client->getAccessToken()));
  }
  $currentEvents = date('Y-m-d', strtotime(date('Y-m-1'))) . 'T00:00:00-23:59';
  $currentMonth = date('F');
  $currentYear = date('Y');
  $endDay = get_number_of_days_in_month(date('m'), $currentYear);
  $endOfMonth = date('Y-m-d', strtotime(date('Y-m-' . $endDay ))) . 'T00:00:00-23:59';


  $calendarId = 'wayko621@gmail.com';
$optParams = array(
  'maxResults' => 100,
  'orderBy' => 'startTime',
   'singleEvents' => TRUE,
  'timeMin' => $currentEvents,
  'timeMax' => $endOfMonth

);
 $results = $service->events->listEvents($calendarId, $optParams);

if (count($results->getItems()) == 0) {
  $Heading =  "No upcoming events found.\n";
} else {
  $Heading =  $currentMonth . " events:\n";

  echo "<div class='calendar'><h2> " . $Heading . "</h2></div>";
  foreach ($results->getItems() as $event) {
    $start = $event->start->dateTime;
    $description = $event->getDescription();
    $formattedDate = date_format(new DateTime($start),"F j, Y - G:i A");
    if (empty($description)){
        $description = "No Description";
    }
    if (empty($start)) {
      $start = 'All Day Event';
    }

   echo "<div class='calendar'><h1>" .$event->getSummary() . "</h1>  <h3 class='getdates'>" . $formattedDate . " </h3><br /><span class='description'>" . $description . "</span></div>";
  }
}
 function get_number_of_days_in_month($month, $year) {
    // Using first day of the month, it doesn't really matter
    $date = $year."-".$month."-1";
    return date("t", strtotime($date));
}

感謝您的幫助

更新:更改為calendar2.php的權限得到了500內部服務器錯誤

這是錯誤日志

[05-Feb-2018 14:55:51 America/New_York] PHP Fatal error:  Call to undefined function GuzzleHttp\Handler\curl_reset() in .../secondavechurch/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 78
[05-Feb-2018 14:56:03 America/New_York] PHP Fatal error:  Call to undefined function GuzzleHttp\Handler\curl_reset() in .../secondavechurch/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 78
[05-Feb-2018 16:20:46 America/Detroit] PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in .../secondavechurch/vendor/google/apiclient/src/Google/Client.php:839
Stack trace:
#0 .../secondavechurch/vendor/google/apiclient/src/Google/Client.php(824): Google_Client->setAuthConfig('CLIENT_SECRET_P...')
#1 .../secondavechurch/oauth2callback.php(13): Google_Client->setAuthConfigFile('CLIENT_SECRET_P...')
#2 {main}
  thrown in .../secondavechurch/vendor/google/apiclient/src/Google/Client.php on line 839

進一步更新:

這是配置錯誤頁面后出現的錯誤

/secondavechurch/calendar2.php?code=4/aajajkajkdnanfnoaono&scope=https://www.googleapis.com/auth/calendar.readonly
Error Code: 403

進一步更新:

將代碼更改為以下內容:

require_once __DIR__.'/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=servicea.json');
  $SCOPES = array(
      Google_Service_Calendar::CALENDAR_READONLY
  );
  $REDIRECT_URI = 'http://' . $_SERVER['HTTP_HOST'] .'/secondavechurch/calendar3.php';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
  $client->setAccessType('offline');
  $client->setApprovalPrompt ("force");
  $client->setRedirectUri($REDIRECT_URI);
  $client->setScopes($SCOPES);
 $service = new Google_Service_Calendar($client);

 $currentEvents = date('Y-m-d', strtotime(date('Y-m-1'))) . 'T00:00:00-23:59';
  $currentMonth = date('F');
  $currentYear = date('Y');
  $endDay = get_number_of_days_in_month(date('m'), $currentYear);
  $endOfMonth = date('Y-m-d', strtotime(date('Y-m-' . $endDay ))) . 'T00:00:00-23:59';

 $calendarId = 'wayko621@gmail.com';
 $optParams = array(
  'maxResults' => 100,
  'orderBy' => 'startTime',
   'singleEvents' => TRUE,
  'timeMin' => $currentEvents,
  'timeMax' => $endOfMonth

);
 $results = $service->events->listEvents($calendarId, $optParams);

 if (count($results->getItems()) == 0) {
  $Heading =  "No upcoming events found.\n";
} else {
  $Heading =  $currentMonth . " events:\n";

  echo "<div class='calendar'><h2> " . $Heading . "</h2></div>";
  foreach ($results->getItems() as $event) {
    $start = $event->start->dateTime;
    $description = $event->getDescription();
    $formattedDate = date_format(new DateTime($start),"F j, Y - G:i A");
    if (empty($description)){
        $description = "No Description";
    }
    if (empty($start)) {
      $start = 'All Day Event';
    }

   echo "<div class='calendar'><h1>" .$event->getSummary() . "</h1>  <h3 class='getdates'>" . $formattedDate . " </h3><br /><span class='description'>" . $description . "</span></div>";
  }
}

 function get_number_of_days_in_month($month, $year) {
    // Using first day of the month, it doesn't really matter
    $date = $year."-".$month."-1";
    return date("t", strtotime($date));
}

開發獲得數據生產一無所獲。 沒有錯誤出現,但是當我使用開發人員工具時,它顯示了500錯誤。 錯誤日志上沒有錯誤。 也許我可以在php中使用try catch來查看它是否確實出現任何錯誤。

此錯誤與Google日歷無關。

此外,在嘗試使用ErrorDocument處理請求時遇到403禁止錯誤。

403是Web服務器錯誤。 它基本上是文件系統錯誤。 Web服務器無權訪問該文件。

我不確定您在哪里運行此文件,但需要確保Web服務器可以訪問該文件。

/secondavechurch/calendar2.php

因此,看來與Composer在服務器方面存在問題。 curl_reset在CurlFactory.php上不起作用。 我注釋了這一行,它奏效了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM