Hi, thanks for reply. Hope these details can help
Please see step 1 to Step 5 in the sample php copde below. This is basicaly the simple.php code in the calendar sample.
I may misunderstand the workflow but it is this.
1) Create Auth URL
2) Get a code and call myself back with a token
3) Set the token
4) Use this token to access the calendar
5) Use the token to give access to the calendar -- this is the part that does not work
/***** SAMPLE PHP CODE *******************/
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>";