我正在尝试将用户登录到 Google 日历,而无需用户输入其姓名和密码。我已经成功设置了 OATH,并且可以通过 PHP 脚本操作他们的日历。但我需要做的是运行一个脚本,然后以某种方式创建一个链接供用户单击以获取访问权限。
请参阅下面示例 php copde 中的第 1 步到第 5 步。这基本上是日历示例中的 simple.php 代码。
我可能会误解工作流程,但就是这样。1) 创建身份验证 URL 2) 获取代码并使用令牌回叫自己 3) 设置令牌 4) 使用此令牌访问日历 5) 使用令牌授予对日历的访问权限 - 这是执行的部分不行
/示例 PHP 代码** * ** * **** /
require_once '../phplibs/gApi/Google_Client.php';
require_once '../phplibs/gApi/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('xxxxxxxxxxxxxxxxxxxxxx');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxxx');
$client->setRedirectUri(xxxxxxxxxxxxxxxxxxxxxxxxxx'); // The redirect is this same script
$client->setDeveloperKey('xxxxxxxxxxxxxxxxx');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
//Step 2
//I have been called back with the code
// so use this to create a token
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
// Step 3
// I have been called back by the redirect in step 2
// but now with an access token
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
//Step 4
// My token exists so now I can work on the users calanders
// I dont want to do this but it does work so proves the auth worked ok
$calList = $cal->calendarList->listCalendarList();
//print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
//Not sure why this happens again as we have a token
$_SESSION['token'] = $client->getAccessToken();
// Step 5
// This is really want I want to do, I want to open the users calendar in a iframe or full page
// However if the user is not logged in they get redirected to the Google login page
// I was expecting that as the token was set this should work.
header('Location: https://www.google.com/calendar/render');
// echo(' <iframe src="https://www.google.com/calendar/embed?src=calendar%40wokingham-theatre.org.uk&ctz=Europe/London" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>');
} else {
//Step 1
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'> Connect to the Google calendar without me having to enter my name and password </a>";