DEV Community

Wesley Chun (@wescpy) for Google Workspace Developers

Posted on • Updated on

Getting started using Google APIs: Workspace & OAuth client IDs (2/3)

Introduction

Are you a developer but a complete beginner using Google APIs? This series is for you because I'm showing you how get started from scratch, beginning with the Google Workspace ("GWS") APIs like Google Drive and Sheets. In the previous post, you took your first steps:

  1. Created or logged into a Google or Gmail account
  2. Created a developer project or selected an existing one, and
  3. Obtained API credentials for GWS APIs, specifically an OAuth client ID

The first pair of steps are required regardless of which type of Google API you wish to use, while the last is only for APIs that access data owned by (human) users, such as GWS APIs. This data can include a user's Gmail messages or files on Google Drive.

Furthermore, the steps above are independent of the development language and the client libraries for the APIs you'll be using. Those are the topics of today's post, getting everything ready before coding:

  1. Select your development language
  2. Select Google API(s) to use
  3. Install client libraries
  4. Enable Google API(s) in DevConsole

For programming language, we've chosen Python and Node.js, and we're going to use the Google Drive API to craft a simple script that dumps out the first 100 files/folders in your Google Drive. Once the final pair of steps are complete, you'll be ready to code.

OPTIONAL: Complete experience via "codelab"

In the section below, I offlink several times to a tutorial (a "Google codelab") I wrote a few years ago introducing developers to coding with GWS APIs. Codelabs are self-paced, hands-on tutorials that lead you step-by-step in accomplishing a task and/or learning a Google API or API feature. Rather than regurgitating the exact same content in these posts, I'll point to specific sections to review or execute.

The codelab consolidates the content from these (three) posts and implements a Python script that displays the first 100 files or folders in a user's Google Drive. We'll eventually end up there, but some of you may learn better/faster by doing an end-to-end, immersive project, in which case, feel free to do the entire codelab at http://g.co/codelabs/gsuite-apis-intro. (If interested, all Google codelabs can be accessed at http://g.co/codelabs.)

Completing the setup

All Google APIs you wish to use in your app requires them to be enabled in the DevConsole. While using Google APIs do not require use of client libraries, they're strongly recommended because they simplify API access, doing a bunch of the heavy-lifting on behalf of developers. Let's start with the latter.

Python 2 vs. 3

Most of you are using Python 3, but there are many of you using both because you have Python 2 code that hasn't been ported yet due to dependencies, resources, etc. If you've got both major releases installed on your development machine, explicitly use version-specific commands like, python2, python3, pip2, and/or pip3 to avoid confusion. To help with migration, the Python code samples in this blog should work on both Python 2 and 3. Keep this in mind and use the appropriate command(s) in the section below.

Install client libraries

Several client libraries are required to use the Drive API: the API client library and one or more authentication ("authn") libraries. The Google APIs client library is what you need to access the Drive API. To install this as well as the required authn client libraries, run the corresponding command for your development language:

  • Python: pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib
  • Node.js: npm install googleapis @google-cloud/local-auth

To confirm all required packages have been installed correctly and ready-to-use, run a 1- or 2-line script that imports those packages:

  • Python: python -c "import googleapiclient, google.auth, google.oauth2, google_auth_oauthlib"
  • Node.js: node -e "require('@google-cloud/local-auth'); require('googleapis')"

If both commands complete without error, they're installed properly and ready-to-use.

Older Python authentication ("authn") libraries

For Python users, be aware the authn libraries are the most recent (2017+). If you have experience with accessing GWS APIs using older libraries, you may wish to continue using them. While deprecated, they have not been shut down yet. To install the older libraries, use the these equivalent commands to install and verify those packages:

  1. pip install -U pip google-api-python-client oauth2client
  2. python -c "import googleapiclient, httplib2, oauth2client"

Google prefers you always use the latest libraries available, especially since they are the ones featured in the current documentation, however you may already have code using the older libraries or someone else's packages using them, or have other dependencies on them. The steps in the codelab linked to in the previous sidebar employs the older libraries, so see that installation page for more details, including specifics about the deprecated libraries and why they may be preferred.

To ensure everyone's on the same page, I'll create the same script twice in the next post, one using the current authn libraries and another with the older ones so you can compare & contrast. Seeing them side-by-side also serves as a guide to migrating to the new libraries — at some point I'll likely write a separate post on just this topic alone. See the codelab's open source repo to get a preview of both scripts.

Enable Google API(s) in DevConsole

The code sample we're focused on requires the Google Drive API, so let's enable that in the DevConsole. Below are two ways of doing so, one for new users while the other is for those already familiar with using the DevConsole:

  1. Go to the DevConsole
  2. Click on the Library tab and search for "Drive"
  3. Click "Enable API" button

If you're new to enabling Google APIs or using the DevConsole, the steps above can be simplified by going to a specific link, http://console.developers.google.com/start/api?id=drive. Once you've logged into your Google account and chosen the correct project, you should be taken automatically to the Drive API library page where you can simply click the "Enable API" button.

After the Drive API has been enabled (or if previously-enabled), you'll see "Manage API" "or "Disable API" instead. For more details, check out the corresponding section of the codelab.

Summary

Part 1 of this blog post series covered the basics of working with Google APIs, specifically GWS APIs as the first set of APIs from Google. In that post, we introduced Google APIs, supported credentials types, and specified you needed two things to use Google APIs: authentication (authn) and authorization (authz). This part 2 post covered the next steps, selecting a development language (or two) as well as decided which APIs to use. After choosing Python and Node.js to access the Google Drive API, the next steps were installing the client libraries, for both API access as well as authn, and finally, enabling the desired APIs in the DevConsole. Ahead in Part 3, you'll actually build your first working code sample based on this foundation you've laid. See you soon!

NEXT POST: Part 3/3 on working with GWS APIs



WESLEY CHUN, MSCS, is a Google Developer Expert (GDE) in Google Cloud (GCP) & Google Workspace (GWS), author of Prentice Hall's bestselling "Core Python" series, co-author of "Python Web Development with Django", and has written for Linux Journal & CNET. He runs CyberWeb specializing in GCP & GWS APIs and serverless platforms, Python & App Engine migrations, and Python training & engineering. Wesley was one of the original Yahoo!Mail engineers and spent 13+ years on various Google product teams, speaking on behalf of their APIs, producing sample apps, codelabs, and videos for serverless migration and GWS developers. He holds degrees in Computer Science, Mathematics, and Music from the University of California, is a Fellow of the Python Software Foundation, and loves to travel to meet developers worldwide at conferences, user group events, and universities. Follow he/him @wescpy & his technical blog. Find this content useful? Contact CyberWeb if you may need help or buy him a coffee (or tea)!

Top comments (0)