Categories
Connected Farm

Interacting with Google Calendar using Node-Red.

As part of my connected farm project I’ve created some Node-Red flows to authenticate and interact with Google Calendar so that I can automate planning and recording tasks such as field observations like breeding service dates, calving due dates or medical withdrawal periods. I am developing flows to pass data between Google Calendar and the on farm farmOS database as well as other sources such as ICBF.com. In this post I will concentrate on the Google Calendar API.

Google uses OAuth2 for authorizing access to its API’s and requires a Client ID, Secret and a Redirect URL for access, see my previous post for details on how to get theses. Documentation from Google on its implementation of OAuth2 can be found here: https://developers.google.com/identity/protocols/oauth2/web-server and the Google Calendar API here: https://developers.google.com/calendar/api/guides/auth.

I created 2 subflows to handle authorization, the first one Google Oauth2 is used for the initial authorization and generates a URL to sign in with a google account, after user authentication is complete an access token is returned to your redirect URL for use in API requests. The Client ID and Secret need to be added into the properties window, the other options are pre-populated but may need updating according to your own Node-Red settings, for example a different port number or endpoint. eg: http://127.0.0.1:1880/endpoint/Google/oauth2.

The second subflow Oauth2 Refresh also needs the Client ID and Secret added to properties and is used to get a new access token without having to reauthenticate the user each time.

With an Inject node and Debug node connected to the bottom output the Google Oauth2 subflow will generate a user authentication URL, this can be copied and pasted into any browser or passed on to another node like an Exec node to open a new browser session.

The generated URL will open a Google Authentication page, after sign in the access token is returned to the redirect URL, which will be processed by the subflow and either an authorization confirmation or rejected page displayed.

The Return or Continue button displayed will redirect you to whatever URL is in the Dashboard field of the properties window. The access token and refresh token are saved as global variables Google_token and Google_refresh.

Some Examples:

This Example uses Node-Red dashboard which can be installed from the manage palette option in the main menu.

It creates 2 simple buttons on the dashboard, clicking on the Google Oauth2 button will redirect you to the Google Authentication page and back to the dashboard when complete. Oauth2 Refresh button will simply request a new access token if the stored refresh token is still valid.

Here are a couple of POST/GET examples to send or retrieve the data from a Google Calendar, they use a Credential node to store the Calendar ID which can also be installed from Manage Palette.

Calendar ID can be found in settings and sharing on any calendar the authenticated user has access to.

All flows in this post can be found at https://github.com/Farmer-Eds-Shed/Node-Red-Google-API-Flows and can be imported to Node-Red by coping the raw contents of any json file into the Import function in the main menu.

Facebook Comments