Automatically log a user on to Google

576 views
Skip to first unread message

Scott Nettleton

unread,
Feb 21, 2013, 1:07:00 PM2/21/13
to oauth...@googlegroups.com
I am trying to log a user into Google Calendar without  the user having to enter their name and password. I have successfully set up OATH and with this I can from a PHP script manipulate their calendar.  But what I need to do is run a script and somehow then create a link for the user to click on to gain access.

Is this possible?

Naveen Agarwal

unread,
Feb 21, 2013, 9:15:28 PM2/21/13
to oauth...@googlegroups.com
Hi Scott,

I'm not sure I understand.
You can use a script to manipulate the calendar using oauth tokens. - great.
You want the script to generate a link and a user clicks on it, she is taken to a browser. Is that right? Which site/domain is the key. In the browser, we want to user user's cookies to manipulate the calendar if the user is on google.com. If you send the user to your website, you could pass the token and then your site could make similar calls as the script was making.
If these are not the cases, please describe the flow in more details.

Thanks

Naveen

On Thu, Feb 21, 2013 at 10:07 AM, Scott Nettleton <scott.ne...@gmail.com> wrote:
I am trying to log a user into Google Calendar without  the user having to enter their name and password. I have successfully set up OATH and with this I can from a PHP script manipulate their calendar.  But what I need to do is run a script and somehow then create a link for the user to click on to gain access.

Is this possible?

--
You received this message because you are subscribed to the Google Groups "Developer Forum for Google API Access using OAuth2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to oauth2-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Scott Nettleton

unread,
Feb 22, 2013, 10:25:55 AM2/22/13
to oauth...@googlegroups.com
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>";

Naveen Agarwal

unread,
Feb 22, 2013, 8:42:14 PM2/22/13
to oauth...@googlegroups.com

// 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

The oauth token does not allow you to manipulate calendar in the browser because in the browser we need cookies and cookies have full account access.

You should use the calendar API to grant access.
perhaps this might help
https://developers.google.com/google-apps/calendar/v3/reference/acl/insert

If none of the existing calendar APIs can do what you are trying to do then, you should try this question in the calendar API forum/group. You may be able to get better answers there.

Thanks

Naveen
Reply all
Reply to author
Forward
0 new messages