Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to the Google Calendar API without the oAuth authentication?

I have been studying the Google Calendar API and the docs on authentication (http://code.google.com/apis/calendar/v3/using.html#auth). It seems that the use case mentioned here is writing an application that accesses the user's calendar. However, I am writing a web page that will access the web site owner's calendar and display just that one calendar's information. So, I don't want the user to enter in their Google account information which is what oAuth wants to do.

Basically, I am looking for a way to access a single private Google calendar and authenticate to it by passing the credentials directly to the service.

There is a similar questions here: How to use OAuth with Google Calendar to access only ONE calendar? that seems to indicate that the poster originally was passing the credentials directly. Is this feature still available? How do you handle the use case I described?

like image 624
Jon Hargett Avatar asked Jan 24 '12 22:01

Jon Hargett


People also ask

How can I use Google API without OAuth?

The strict answer to "Is there a way to do this without OAuth and just an API key and/or client id and secret?" is no. However, you can achieve what you are looking for using OAuth. You simply need to store a Refresh Token, which you can then use any time to request an Auth Token to access your gmail.

How do I access Google Calendar API?

Enable API accessIn the API Manager pane, click Library to see the list of available APIs. In the Google Apps APIs list, scroll down to click or search for Calendar API, then on the API page, click Enable.

How do I authenticate Google API?

With a user account, you can authenticate to Google APIs and services in the following ways: Use the gcloud CLI to set up Application Default Credentials (ADC). Use the gcloud CLI to generate access tokens. Use your user credentials to impersonate a service account.


2 Answers

If I'm not wrong, they've launched Service Account for this now: https://developers.google.com/accounts/docs/OAuth2ServiceAccount

Edit:

Here's the modification from their Prediction API

    session_start();     require_once "google-api-php-client/src/Google_Client.php";     require_once "google-api-php-client/src/contrib/Google_CalendarService.php";      const CLIENT_ID = '...';     const SERVICE_ACCOUNT_NAME = '...';      // Make sure you keep your key.p12 file in a secure location, and isn't     // readable by others.     const KEY_FILE = '...';      $client = new Google_Client();     $client->setApplicationName("...");       if (isset($_SESSION['token'])) {      $client->setAccessToken($_SESSION['token']);     }      // Load the key in PKCS 12 format (you need to download this from the     // Google API Console when the service account was created.     $key = file_get_contents(KEY_FILE);     $client->setAssertionCredentials(new Google_AssertionCredentials(         SERVICE_ACCOUNT_NAME,         array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),         $key)     );      $client->setClientId(CLIENT_ID);     $service = new Google_CalendarService($client);      //Save token in session     if ($client->getAccessToken()) {       $_SESSION['token'] = $client->getAccessToken();     }      //And now you can use the code in their PHP examples, like: $service->events->listEvents(...) 
like image 117
gX. Avatar answered Sep 18 '22 19:09

gX.


I have the same problem. I found this 3 part tutorial that makes exactly what you want . Web page for bookings
You create a web page that writes directly into your own calendar (not the users) and that is why users don't have to give you permission to access their calendar.
This solution works for me, but you have to manage it directly with REST.

I am looking for a way to use google api php client because it seems a bit simpler. But I have not yet found a way to how to properly edit the google-api-php-client/src/config.php file to work in a way where you authenticate yourself not the actual user, because you need access to your calendar. If anyone has an idea or a script that would do that I would be very grateful.

like image 29
Andrej Bergant Avatar answered Sep 17 '22 19:09

Andrej Bergant