Hi everyone, hope you are doing good. This article is all about learning how to create a project in google console and understanding how to use this project’s credentials to build our application over Google APIs.
Creating a project in google console as a developer is the most important step. Because without that you won’t be able to use Google APIs to build your application (i.e won’t be able to retrieve google application data programmatically).
Without wasting any time let’s dive into our “4 step guide to create projects in Google console and generate access token refresh token for Google APIs”.
1. Create a Project:
The first step we need to complete and it’s the base of all other steps/processes – to create a project in Google Console as a developer.
Go to Google Console in order to create a project. It might ask you to register or login.
As soon as you log in to google console you will see a default project named “My Project”. If you want you can use the same project for practice purposes or create a new one, whichever suits you best.
To create a new Project click on the down arrow at the side of the “My Project” label. A pop up will come, then look for “New Project” and click on it.
Refer below image for above steps.
After clicking on “New Project”. You will be redirected to a page asking for the Project name and location. Give a project name now, I gave “Setup Guide Project“. The location of the organization is optional.
You will notice “project id” as soon as you give a name to your project, if you want you can edit that too, I have used default. After completing the above process click on the “CREATE” button.
2. Enable API for the Project:
Now we are at the step where we are going to enable google API according to your work/college requirement. Select the project, either a newly created or already existing, whose credentials you are willing to use further in the application you’re going to develop.
Click on “Enable APIS and SERVICES”. As shown in image below.
You are directed to another page with an entire list of APIs you can enable for the project. If you already know which API you need, search for that API – by typing the service name in the search bar. If you are not aware of which API to use, go through the list of APIs you can see on your screen, select one that best fits your requirement.
The below screenshot gives an idea of how that page looks, there are many more APIs available than what you see in the image.
For this project, I will use Google Sheet API. Select any API you want according to your need and click on the “ENABLE” button to enable that API for your project. Note that you can enable multiple API for the same project, as far as it meets your project requirement.
3. Create Credential for Project:
Look for the “credentials” tab in the left corner and click on it.
First, you need to complete the configure of Consent Screen, to do that click on CONFIGURE CONSENT SCREEN.
I have selected the option “External”. To select the option “Internal ” you need to be a G suit user.
After going through the above consent screen process, come back to the “credentials” tab. And click on “CREATE CREDENTIALS”.
There are two ways to identify your application: using an OAuth 2.0 token (which also authorizes the request) and/or using the application’s API key. Here’s how to determine which of those options to use:
- If the request requires authorization (such as a request for an individual’s private data), then the application must provide an OAuth 2.0 token with the request. The application may also provide the API key, but it doesn’t have to.
- If the request doesn’t require authorization (such as a request for public data), then the application must provide either the API key or an OAuth 2.0 token, or both—whatever option is most convenient for you.
For more details on the Authorization request click here.
I’m going to use the “OAuth Client ID” i.e., OAuth 2.0 method of authorizing my app.
Select an application type (I went for web application) and give a name to your credentials. Authorize redirect URI – if you know you can add them now, I’m going to leave it blank and will add URI later.
After clicking on “create”, a pop with the Client ID and Client Secret will come.
Note them down – keep it secure. I recommend creating a JSON file to store this and upcoming credentials. Don’t worry if you have ignored the popup, you can still get credential by clicking on the credential name under the OAuth 2.0 Clients ID section.
4. Getting Access Token and Refresh Token:
There are two ways to generate a token. one using Google 2.0 Playground service and another using the Postman application. We are going to see both.
4.1. Generating Token using OAuth 2.0 Playground.
We need to make sure the access token is authorized for our app specifically. You can do that by typing in your own application information (the client id and client secret I told you to keep) in the “Setting” section:
Enter your Client ID and Client secret above.
Each access token is only granted for a restricted purpose called “Scope” by Google. For the Google sheet, I have found the scope we need.
As you can see there are two scopes. We are going to paste this scope directly in the area pointed by the orange arrow below, with space-separated. If you don’t have a scope already, you can search that scope through the provided list from the area pointed by the green arrow.
The most important step here you need to remember is to add the redirect URI “https://developers.google.com/oauthplayground” in the “Authorized redirect URIs” section of the credentials app we created above in step 3. As shown below.
After saving the redirect URI come back to the “Google Developer” Screen and click on the “Authorize APIs button. Then, you will be redirected to your usual Google login screen, use the account from which you want to give google sheet access to your application.
After that, you will come back to your Google Developer screen with the Authorization code. Click on “exchange authorization code for the token” and you should see your access token and refresh token.
4.2. Generating Token using Postman Application.
Here we are going to use a postman to generate a token. To know more about Postman visit (link).
If you don’t have a postman set up on your device refer to this (link) to setup Postman.
After setting up postman. The below image gives a glimpse of how its overall dashboard looks.
We are going to follow best practice here (.i.e. first will create a collection and under-collection will create a request to get a token).
First click on “New Collections”, a popup asking for collection name and description will appear. Give a name to your collection and click on “Create”.
Now it’s time to create a request to generate tokens that will be used from our application. To do so click on “…” and select “Add Request”.
A popup will appear, give in the request name as “ga-sheet-oauth” (you can you any other name) and create the request by clicking on the “Save to Google Sheet Demo” (<- your collection name) button.
After creating a request click on “Authorization Tab”.
Fill up the above details and click on “Get New Access Token”. You will be redirected to choose an account, give all permission and click on “open Postman”.
The most important step here you need to remember is to add the redirect URI “https://oauth.pstmn.io/v1/callback” in the “Authorized Redirect URIs” section of the credentials app we created above in step 3. As shown below.
On Authentication successful or completed postman will show a pop up with all needed credentials. Keep a note of the access token.
NOTE: Postman doesn’t give Refresh Tokens that’s the only disadvantage according to me.
Hope I have made the entire process of creating a project in Google Console for APIs and generating access token and refresh token simple enough.
If you have any questions or comments feel free to reach me at.