[英]403 Forbidden error with Google Calendar
I been trying to access my Google calendar using the calendar api. 我一直在尝试使用Calendar API访问我的Google日历。 On my development machine I am able to access the data and display the events on the calendar, but when I push it to the production server I get the following error after giving permission access to the calendar. 在我的开发机上,我可以访问数据并在日历上显示事件,但是当我将其推送到生产服务器时,在授予对日历的访问权限后会出现以下错误。
Forbidden 禁止的
You don't have permission to access /secondavechurch/calendar2.php on this server. 您无权访问此服务器上的/secondavechurch/calendar2.php。
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. 此外,在尝试使用ErrorDocument处理请求时遇到403禁止错误。
I made a service account and gave rights to access the calendar to see if it's was the issue, but still get the same error. 我注册了一个服务帐户,并授予访问日历的权限,以查看是否是问题所在,但仍然出现相同的错误。
Here is my code 这是我的代码
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));
}
Thanks for the help with this 感谢您的帮助
Update: Changed permission to calendar2.php Got 500 Internal Server Error 更新:更改为calendar2.php的权限得到了500内部服务器错误
Here is the error log 这是错误日志
[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
Further Update: 进一步更新:
This is the error I am getting after configuring error page 这是配置错误页面后出现的错误
/secondavechurch/calendar2.php?code=4/aajajkajkdnanfnoaono&scope=https://www.googleapis.com/auth/calendar.readonly
Error Code: 403
Further Update: 进一步更新:
Changed code to the following: 将代码更改为以下内容:
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));
}
development gets data production doesn't get anything. 开发获得数据生产一无所获。 No error comes up, but when I go to developer tools it shows a 500 error. 没有错误出现,但是当我使用开发人员工具时,它显示了500错误。 No error on error log. 错误日志上没有错误。 Maybe I can use try catch in php to see if it does get any error. 也许我可以在php中使用try catch来查看它是否确实出现任何错误。
This error has nothing to do with Google calendar. 此错误与Google日历无关。
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request. 此外,在尝试使用ErrorDocument处理请求时遇到403禁止错误。
403 is a web server error. 403是Web服务器错误。 Its basically a file system error. 它基本上是文件系统错误。 The web server doesn't have access to the file. Web服务器无权访问该文件。
I am not sure where you are running this but you need to make sure that the web server has access to the file in question. 我不确定您在哪里运行此文件,但需要确保Web服务器可以访问该文件。
/secondavechurch/calendar2.php
So it seems that it was an issue with Composer on the sever side. 因此,看来与Composer在服务器方面存在问题。 curl_reset wasn't working on the on CurlFactory.php. curl_reset在CurlFactory.php上不起作用。 I commented out the line and it worked. 我注释了这一行,它奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.