Abandon Facebook II

Synchronizing Facebook with Google Calendar

Alexander Mancevice
6 min readDec 31, 2018

Disclaimer — I was not paid to write this piece; nor do I have any past, present, or promised affiliations with the companies mentioned herein.

This is the second part of a two-part series. Read the first part here.

This piece is targeted at a fairly technically-savvy audience; familiarity with REST APIs, Python, Terraform, Heroku and/or AWS is recommended.

At this point you may be resolved to translate your calendar from facebook to Google (or something else like it), but what tools are required? The two most evident obstacles in transitioning will be finding a way to synchronize events between facebook and Google and providing a simple method to subscribe to the calendar with or without a Google account. Neither is impossible, but some technical skill is required.

The most technically demanding problem is the former and the one discussed in this essay; the problem of event synchronization. It will require a fair amount of setup to acquire the proper access tokens to read from facebook and write to Google. You will also need to choose a platform — such as Heroku or AWS — where the sync job will run automatically on some kind of schedule. I have authored a set of tools to help with these tasks.

The core tool is written in Python and called fest. The purpose of the tool is to read events from facebook and add them into Google Calendar. Changes to events are detected by taking a hash of the JSON facebook event object and storing this value in the extended attributes of the Google Calendar object. Events that are found in Google Calendar but are no-longer available in facebook are deleted. Take a look at the README of the tool to familiarize yourself with it.

But first, some assembly may be required.

facebook

This section will walk you through the process to obtain a long-lived Page Access Token.

Note that there is an assumption here that the facebook page you are attempting to access is public.

Grant Page Access

Before beginning, ensure your facebook account is granted admin access to the desired facebook page. In technical terms, your user must have the manage_pages or pages_show_list permissions in order for subsequent steps to succeed.

Get User Token

Log into the Graph API access token debugger tool. At the top of the screen you will find your User Access Token. You will use this token to get a Page Access Token.

Get Page Token

Open a terminal window and execute the following cURL command:

curl 'https://graph.facebook.com/{page-id}?fields=access_token&access_token={user-access-token}'

Where {page-id} and {user-access-token} are filled-in with the ID/alias of your facebook page and the token from the previous step, respectively.

The response should take the following form:

{
"access_token": "{page-access-token}",
"id": "{page-id}"
}

Copy the value of the access_token field and paste back into the access token debugger tool and click the Debug button.

The type of the pasted token should now read Page. Click the Extend access token button to generate a new token that will never expire.

Save this token in a safe place as it will be necessary for subsequent steps.

Google

This section will walk you through creating a Google Project & service account that will be used to write to a Google Calendar.

Create a Project

Use the new project wizard provided by Google to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.

Add a Service Account

On the Add credentials to your project page, click the Cancel button.

Click the Create credentials dropdown on the subsequent screen and select Service account key.

Under the Service account dropdown, select New service account. Give the service account and name and a role of Project > Viewer.

Select JSON as the Key type and click Create.

The client_secret.json file will automatically be downloaded to your computer. Put this file in a safe location. Do not lose it as it will not be regenerated.

Share Your Calendar

In the client_secret.json file you will find an email address in the client_email field of the document. Copy this address to your clipboard.

Follow the instructions on sharing your Google Calendar using the copied address as the collaborator. Ensure that the service account is granted permission to Make changes to events.

Your service account will now be able to add, edit, or delete events from facebook.

Deployment

With your facebook Page Access Token and Google Service Account you must now choose how to deploy fest as a cloud service. Several out-of-the-box solutions are provided, two for heroku and one for AWS.

Heroku

Deploying to Heroku is the simpler of the two options I will describe. It can be done as easily as clicking the Deploy to Heroku button in the Heroku section of the fest documentation and pasting the requested values for the app. This will create an application in Heroku with the scheduler addon.

Alternatively, you can use terraform to deploy your app to heroku.

module facebook_gcal_sync {
source = "amancevice/facebook_gcal_sync/heroku"
app_name = "some-unique-name"
facebook_page_id = "<page-id>"
facebook_page_token = "<token>"
google_calendar_id = "<google-calendar-id>"
google_credentials_file = "/path/to/client_secret.json"
}

In either case, once the app is deployed, use the scheduler add on to schedule a regular run of the synchronization script and copy events from facebook to Google Calendar.

Amazon Web Services

AWS is a monster. Its parent company is certainly no friend to the socially conscious, but its reach is undeniable. You may even elect to use a different service thinking that you are absolved only to find that it, too, runs on Amazon.

Heroku was AWS all along!

Given its expansive presence in the cloud ecosystem and its fairly permissive free-tier for serverless applications, this is the platform I recommend using.

As with Heroku, a terraform module is provided to help get started synchronizing facebook events in the cloud.

module facebook_gcal_sync_secrets {
source = "amancevice/facebook_gcal_sync_secrets/aws"
facebook_page_id = "<page-id>"
facebook_page_token = "<token>"
google_calendar_id = "<google-calendar-id>"
google_credentials_file = "/path/to/client_secret.json"
}
module facebook_gcal_sync {
source = "amancevice/facebook-gcal-sync/aws"
facebook_page_id = "<facebook-page-id>"
facebook_secret_name = "${module.secrets.facebook_secret_name}"
google_calendar_id = "<google-calendar-id>"
google_secret_name = "${module.secrets.google_secret_name}"
}

You can also see an advanced example of this tool in production by visiting the Boston DSA GitHub project. This project includes extra functionality to publish events to our chapter’s Slack workspace as well as any errors in execution.

--

--