Google Client API for ProcessWire
This module manages and simplifies the authentication and setup between
Google service APIs and ProcessWire, and it is handled in the module settings.
The module also currently provides common API calls for Google Sheets and
Google Calendar via dedicated classes, enabling a simpler interface to these
services than is available through Googleâs client libraries., (*1)
This package also includes the MarkupGoogleCalendar module, which is useful
for rendering calendars in your site, but also serves as a good demonstration
of using the GoogleClientAPI module., (*2)
Requirements
- ProcessWire 3.0.123 or newer
- PHP 5.4 or newer (PHP 7+ recommended)
- A Google account that you want to enable APIs for
Installation
Google sometimes changes things around in their APIs interface, though the essence remains the same.
These instructions have gone through 3 iterations since 2015 to keep them up to date with Google's
changes, and the current iteration was last updated July 22, 2019. If you encounter significant
differences on the Google side, please let us know., (*3)
Step A: Install module files
-
Download the moduleâs ZIP file,
unzip and place the files in a new directory named:
/site/modules/GoogleClientAPI/
, (*4)
-
Login to your ProcessWire admin and go to: Modules > Refresh., (*5)
-
Click âInstallâ next to the GoogleClientAPI module (which should appear on
the âSiteâ tab)., (*6)
-
You should now be at the moduleâs configuration screen, remain here for the next step., (*7)
Step B: Install Google Client library
-
On the module configuration screen, you should now see a checkbox giving you the option to
install the Google PHP API Client library., (*8)
-
Proceed with installing the library, either by checking the box and clicking Submit, or
downloading and installing yourself to the location it provides., (*9)
-
Note that the library is quite large, so it may take a few minutes to complete installation., (*10)
Step C: Enable APIs in Google Console
-
Open a new/window tab in your browser, go to Google Console
and login (if not already). It may ask you to agree to terms of serviceâcheck the box and continue., (*11)
-
Now you will be in the âGoogle API and Servicesâ Dashboard. It will ask you to select a project.
Chances are you don't yet have one, so click âCreateâ., (*12)
-
You should now be at the âNew Projectâ screen. Optionally modify the project name or location, or
just accept the defaults. Then click the âCreateâ button to continue., (*13)
In my case, I entered "ProcessWire website services" for the project name and left the
location as-is., (*14)
-
Now you'll be back at the Dashboard and there will be a link that says
âEnable APIs and Servicesâ, go ahead and click that link to continue., (*15)
-
The next screen will list all the Google APIs and services. Click on the API/service that youâd
like to enable., (*16)
-
On the screen that follows the service you clicked, click the âEnableâ button to enable the
API for that service., (*17)
Step D: Creating credentials in Google Console
-
After enabling your chosen API service(s), the next screen will show a notification that says:, (*18)
To use this API, you may need credentials. Click 'Create credentials' to get started., (*19)
Go ahead and click the âCreate credentialsâ button as it suggests and fill in the following
inputs that it displays to you:, (*20)
-
Which API are you using? â Select the API you want to use.
-
Where will you be calling the API from? â Select âWeb serverâ.
-
What data will you be accessing? â Select âUser dataâ., (*21)
Then click the âWhat credentials do I need?â button., (*22)
- After clicking the button above, it may pop up a box that says âSet up OAuth consent screenâ,
in which case you should click the âSet up consent screenâ button. The âOAuth consent
screenâ has several inputs, but you don't need to fill them all in if you don't want to.
I do recommend completing the following though:
-
Application name: You can enter whatever you'd like here, but in my case I entered:
âProcessWire Google Client APIâ., (*23)
-
Application logo: you can leave this blank., (*24)
-
Support email: you can accept the default value., (*25)
-
Scopes for Google APIs: leave as-is, you'll be completing this part in ProcessWire., (*26)
-
Authorized domains: Enter the domain name where your website is running and hit enter.
If it will be running at other domains, enter them as well and hit enter for each., (*27)
The next 3 inputs are only formalities. Only you will be seeing them, so they aren't really
applicable to our project, but we have to fill them in anyway. You can put in any URLs on
your website that you want to., (*28)
-
Application homepage: Enter your websiteâs homepage URL. This will probably the the same
as your first âauthorized domainâ but with an http://
or https://
in front of it., (*29)
-
Application privacy policy link: Enter a link to your website privacy policy, or some
other URL in your website, if you don't have a privacy policy. Only you will see it., (*30)
-
Application terms of service: Enter a URL or leave it blank., (*31)
After completing the above inputs, click the âSaveâ button., (*32)
- The next screen will present you with a new âCreate Credentialsâ button.
Click that button and it will reveal a drop down menu, select âOAuth client IDâ.
Complete these inputs on the screen that follows:
-
Application type: Select âWeb applicationâ, (*33)
-
Name: Accept the default âWeb client 1â, or enter whatever youâd like., (*34)
-
Authorized JavaScript origins: You can leave this blank., (*35)
-
Authorized redirect URIs: To get this value, you'll need switch windows/tabs to go back
to your ProcessWire Admin in: Modules > Configure > GoogleClientAPI. There will be a
notification at the top that says this:
~~~~~
Your âAuthorized redirect URIâ (for Google) is:
https://domain.com/processwire/module/edit?name=GoogleClientAPI
~~~~~, (*36)
Copy the URL out of the notification that appears on your screen and paste it into the
âAuthorized redirect URIsâ input on Googleâs screen, and hit ENTER., (*37)
-
If you see a âRefreshâ button, go ahead and click it., (*38)
- When you've filled in all of the above inputs, you should see a âCreate OAuth Client IDâ
button, please go ahead and click it to continue, and move on to step E below.
Step E: Download credentials JSON file from Google
-
If the next screen says âDownload Credentialsâ, go ahead and click the âDownloadâ
button now. It will download a client_id.json
file (or some other named JSON file) to
your computer., (*39)
If you don't see a download option here, itâs okay to proceed, you'll see it on the next step., (*40)
-
Click the âDoneâ button. You will now be at the main âCredentialsâ screen which lists
your OAuth clients., (*41)
-
If you haven't yet downloaded the JSON file, click the little download icon that appears on
the right side of the âOAuth 2.0 Client IDsâ table on this screen to download the file. Note
the location of the file, or go ahead and load it into a text editor now. We'll be returning
to it shortly in step F below., (*42)
-
You are done with Google Console though please stay logged in to it. For the next step we'll
be going back into the ProcessWire admin., (*43)
Step F: Authenticating ProcessWire with Google
Please note: In this step, even though you'll be in ProcessWire, you'll want to be sure you are still logged
in with the Google account that you were using in step 3., (*44)
- Now we will fill in settings on the ProcessWire side. You'll want to be in the GoogleClientAPI
module settings in your ProcessWire admin at: Modules > Configure > GoogleClientAPI.
Complete the following inputs in the module settings:
-
Application name: Enter an application name. Likely you want the same one you entered
into Google, i.e. âProcessWire Google Client APIâ, or whatever you decided., (*45)
-
Scopes (one per line): for this field you are going to want to paste in one or more
scopes (which look like URLs). The scopes are what specifies the permissions you want for
the APIs you have enabled. Determine what scopes you will be using and paste them into
this field. There's a good chance you'll only have one.
View all available scopes.
Examples of scopes include:, (*46)
https://www.googleapis.com/auth/calendar.readonly
for read-only access to calendars.
https://www.googleapis.com/auth/spreadsheets
for full access to Google Sheets spreadsheets.
https://www.googleapis.com/auth/gmail.send
for access to send email on your behalf., (*47)
-
Authentication config / client secret JSON: Open/load the JSON file that you downloaded
earlier into a text editor. Select all, copy, and paste that JSON into this field.
Click the âSubmitâ button to save the module settings., (*48)
- After clicking the Submit button in the previous step, you should now find yourself at a
Google screen asking you for permission to access the requested services. Confirm all access.
-
Depending on the scope(s) you requested, it may tell you that your app is from an unverified
developer and encourage you to back out. It might even look like a Google error screen, but
don't worry, all is well â find the link to proceed, hidden at the bottom. Unless you aren't
sure if you trust yourself, keep moving forward with whatever prompts it asks to enable access., (*49)
-
Once you have confirmed the access, it will return you to the GoogleClientAPI module configuration
screen in ProcessWire., (*50)
- Your GoogleClientAPI module is now configured and ready to test!
Markup Google Calendar module
This add-on helper module renders a calendar with data from Google. This module demonstrates
use of and requires the GoogleClientAPI module, which must be installed and configured
prior to using this module. It requires the following scope in GoogleClientAPI:
https://www.googleapis.com/auth/calendar.readonly
, (*51)
See the _mgc-event.php
file which is what handles the output markup. You should
copy this file to /site/templates/_mgc-event.php
and modify it as you see fit.
If you do not copy to your /site/templates/ directory then it will use the
default one in the module directory., (*52)
Please note that all render methods cache output by default for 1 hour. You can
change this by adjusting the $cacheExpire property of the module., (*53)
Usage
<?php
$cal = $modules->get('MarkupGoogleCalendar');
$cal->calendarID = 'your-calendar-id';
$cal->cacheExpire = 3600; // how many seconds to cache output (default=3600)
$cal->maxResults = 100; // maximum number of results to render (default=100)
// use any one of the following
echo $cal->renderMonth(); // render events for this month
echo $cal->renderMonth($month, $year); // render events for given month
echo $cal->renderDay(); // render events for today
echo $cal->renderDay($day, $month, $year); // render events for given day
echo $cal->renderUpcoming(10); // render next 10 upcoming events
echo $cal->renderRange($timeMin, $timeMax); // render events between given min/max dates/times
More details and options can be found in the phpdoc comments for each
of the above mentioned methods., (*54)