Это мой код.
Код: Выделить всё
index.phpКод: Выделить всё
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfig('xxx.json');
$client->setAccessType("offline");
$client->addScope('https://www.googleapis.com/auth/calendar.events.readonly');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$service = new Google_Service_Calendar($client);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "No upcoming events found.
";
} else {
print "Upcoming events:
";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)
", $event->getSummary(), $start);
}
}
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/xxx/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
< /code>
oauth2callback.phprequire_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfigFile('xxx.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/xxx/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/calenda ... s.readonly');
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/xxx/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
< /code>
I have two problems:
- When Google asks for my account's OAuth it says my app is not safe. How could I fix that?
- If I change web browser, Google asks for my OAuth again (which others are not supposed to know), but I wanted an automatic authorization that doesn't require OAuth every time and that means even when my account is 'offline'.
Подробнее здесь: https://stackoverflow.com/questions/761 ... ng-offline
Мобильная версия