A large chunk of the documentation is dedicated to authorisation and authentication. APIs allow systems to talk to each other. But you don't want just anyone to be able to access secure data via an API. We need some way of verifying a) who is trying to access that data and b) which data they should be allowed to access.
Think about a public-use computer, like one at a library or a school or university. Each user has their own username and login – but into their own account. The username and password means the computer knows who is trying to access its hard drive and what they should be able to see. And if someone uses an admin login, the computer also knows that this person should be allowed greater control over the computer. That's the who and the what.
In the sections below, let's go through some core concepts that will explain what's happening in the Gmail API.
Authorisation: the What
As with many aspects of APIs, there is no one-size-fits-all for authorisation. However, one of the most common authorisation frameworks is OAuth, or "Open Authorisation".
With OAuth, two secure systems can talk to each other without needing to share account passwords. You've probably used it already, even if you weren't aware of it. Let's say you've just booked an event on a website and there's a button to add it to your Google Calendar. When you click it, you may get a pop up confirming that you want to allow the events website to access your calendar. Oftentimes, that will be done through OAuth.
The crucial part here is that your passwords are kept more secure, because you don't need to give the events website your Google password directly. As you can imagine, digitally sending a password to multiple third-parties could be a recipe for disaster. For our purposes, this means that we don't have to use our actual google username and password in the API call; other mechanisms are used to provide that layer of security.
OAuth 2.0
The Gmail API specifically uses a version of OAuth called OAuth 2.0. As one might guess from the name, OAuth 2.0 is the thrilling sequel to OAuth 1.0, generally more secure and more streamlined than its predecessor. Frankly, as a casual API-user rather than a data security engineer, we don't actually need to know the full details of what's happening under the hood here. So here I'll just cover the general principles.
As explained in either system, two secure systems can talk to each other without the need to share usernames and passwords. Instead, they're sharing tokens. There are several intermediate steps here and different types of tokens going to different places in 1.0 and 2.0 respectively, but the most important of these is the access token. The access token is only granted when the right hoops have been jumped through, e.g. the authentication (see the relevant section below) and the user's approval. This is what finally allows that secure data to be shared: the final step.
Refresh tokens
One of several things that make OAuth 2.0 'better' (in quotations so as to avoid offending the die-hard 1.0 stans) is the existence of refresh tokens. In OAuth 1.0, an access token once received could be stored for over a year. However, 2.0 introduces an expiry date for access tokens – reducing the danger from leaked access tokens – and introduces alongside it the refresh token. This can be optionally requested alongside the access token. Once the access token has expired, the refresh token can – you guessed it – refresh that token. So long as the refresh token exists, new access tokens can be generated without reauthorising.
But, you may ask, what is the refresh token gets leaked? Couldn't someone just use that to keep getting new access tokens? Why would having the refresh token be any more secure in that scenario? And that's a good point: if the user has both, that's not necessarily any more secure. But having them as two separate tokens means they can be accessed differently. For instance, a larger pool of users could provided with short-lived access tokens upon request without ever seeing the refresh token, reducing the likelihood of leaks. If handled appropriately, refresh tokens can be more secure than a longer-lived access token.
For this project, I'm going to be accessing the API from a personal laptop, so I'm not doing anything fancy with tokens here. Obtaining a refresh token means that I don't have to reauthorise every time I pull data, which means less work for me!
Scopes
Even if a system jumps through the various security hoops to get a valid access token, that doesn't necessarily mean we want to throw open the doors and access absolutely everything. After all, one of the benefits of an API call is that servers only have to share the pre-defined, pre-allowed information, which is cleaner and more secure.
In many applications, what defines which information the requesting server has access too is the authorisation scope. The scope would generally be defined within the API call itself, telling the recipient server what information it wants authorisation to request. The user is then required to provide permissions for precisely what the scope has defined in order for an access token to be provided.
These scopes would be defined within the API specification, as the kinds of restrictions that would be useful and/or necessary are highly server-dependent. Since it's topical, let's use the Gmail API as an example (actual scopes defined here). If I specify a scope which allows read-only access to emails, the user will be asked during authorisation to allow this specific access. So long as they agree, the access token is provided and I can access the information I need. But if I want to send or delete emails, I'd need to provide another scope for that and reauthorise.
Multiple scopes can be provided, but their permissions must match what you're actually trying to do. But best practice is to choose the most minimal scope possible.
Authentication: the Who
When a human tries to access a secure system, usually you would use a username and password. While a username may be public, being able to provide the password as well tells the system that you are indeed who you say you are – provided you've kept your password secure, of course. This is what's meant by authentication.
Different APIs will handle authentication differently. Some are completely open-access, and won't require access at all. For secure APIs like Gmail's, some kind of key is required. An API key serves the equivalent function for the servers as the username and password do for the human user. Typically, this will be a long string of letters and numbers.
Authentication with OAuth
Some authentication protocols have greater requirements than a single API key. In particular, OAuth requires a Client ID and a Client Secret.
A Client ID is more or less equivalent to a username, a system's public unique identifier. It allows the recipient server to see which server is sending the request. Because it's not for humans, it often looks like a fairly random string of letters and/or numbers, but different systems will generate their Client IDs differently. For a salient example, Google's look something like this:

That string of numbers will be unique to each requesting server. Meanwhile, a Client Secret is the password. This should be known only to the two systems involved.
It's very important to keep an. In fact, Github by default doesn't let you commit IDs and secrets to a repo as a security measure. After all, if you accidentally put your Gmail Client ID and Secret on a public Github repository, any Tom, Dick, or Harry could have access to your emails. Not great news!
Implementation for Gmail
The Python quickstart guide gives very clear step-by-step instructions on how to set this up. Rather than just recapping these instructions here, I'm going to instead break them down in relation to the concepts explained in this blog.
1. Enabling the API
Fairly self-explanatory! Google has many APIs available to use but most aren't enabled by default. You will need to create a project first to see the options to enable or disable APIs. From there, you can search through the available APIs and enable the Gmail API.

2. Configuring OAuth
As mentioned above, the Gmail API uses OAuth 2.0 for authorisation. This needs to be configured before it can be used. You'll first need to fill out the appropriate information, then you can consent to the terms of service for the API. Here, we're defining what is going to be accessing the API in this project – here, just Python.
After that, you will have set up the initial OAuth configuration.
3. Setting up the Client
We've set up the initial OAuth configuration; now we need to set up the Client. We'll need a Client ID and Client Secret to access OAuth.
First, we'll create the client:

After pressing Create, you should see a pop-up containing your Client ID and Client Secret. Note: do not just close this pop-up immediately. You will not be able to access your Client Secret later. This is for security reasons: even if someone else somehow finds the webpage later, they still won't be able to find your Client Secret.
Download the json file and keep it somewhere sensible where you can find it again. This json file will contain all the information necessary for the given python code to obtain access and refresh tokens, in the correct structure. With any luck, you won't need to touch the json at all.
4. Success!
After all of that, you'll have an enabled API for your Google account and have obtained your Client ID and Secret. Now Gmail knows that a desktop application might try to access it using that particular Client ID and Secret, so it should be happy to provide an access token once requested. Success!
The steps configuring the Gmail API are thankfully laid out very clearly, but note that not every secure API will be so forgiving. Understanding the general principles behind what's happening will be crucial in those cases.
Note that we haven't yet defined scopes. Thankfully, we can do this later as we move into Python in the next blog in the series.

