Step-by-Step Guide: Creating a Google Console Project for API Integration
Hi everyone, I hope you are doing well. This article is all about learning how to create a project in Google Console and understanding how to use this project credentials to build our application over Google APIs. Creating a Google Console project is the first step to using Google APIs to build your application. Without a project, you won’t be able to retrieve Google application data programmatically. This blog post will guide you through the process of creating a Google Console project and generating access tokens and refresh tokens for Google APIs. 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”.
What are Google APIs?
Google APIs are a collection of services that allow you to access Google data and functionality from your applications. For example, you can use the Google Maps API to display maps in your application, or the Google Drive API to allow users to store and share files in your application.
To use Google APIs, you need to create a Google Console project and generate an access token. An access token is a short-lived credential that allows your application to access Google APIs. Access tokens expire after a certain period of time, so you need to refresh them periodically.
1. Creating a Project in Google Console
To get started with the foundational step, which serves as the bedrock for all subsequent processes, follow these instructions to create a project in Google Console as a developer. Here’s how you can do it:
Step 1: Begin by navigating to the Google Console. If you’re not already logged in, you may be prompted to either register or log in.
Step 2: Upon successful login, you’ll find a default project labeled ‘My Project‘. You have the option to use this project for practice purposes or create a new one that aligns with your specific requirements.
Step 3: To initiate the creation of a new project, simply click on the downward arrow located next to the ‘My Project’ label. This action will prompt a pop-up menu to appear.
Step 4: Within the pop-up menu, look for the ‘New Project’ option and click on it.
Refer to the below image for the above steps.
After clicking on ‘New Project‘, you’ll be directed to a page where you need to provide a Project name. For our example, let’s use the ‘Setup Guide Project’. The location of the organization is optional. You’ll notice the ‘project id’ as soon as you enter a name for your project. You can edit the ‘project id’ if necessary, but using the default one is just fine. Once you’ve completed the steps mentioned above, click on the ‘CREATE’ button.
2. Enabling APIs for your Google Project
The next crucial step is enabling Google APIs based on your specific work or college requirements. Whether you’re working with a newly created project or an existing one, the credentials you generate here will be essential for your upcoming application development. Here’s how to proceed:
Begin by selecting the project you intend to use for your application.
Click on “Enable APIs and Services”, as illustrated in the image below:
After initiating the process, you’ll be directed to a page featuring an extensive list of APIs available for your project. If you already know the specific API you require, simply use the search bar and type in the service name. However, if you’re unsure about which API to choose, take a moment to explore the list of available options on your screen, and select the one that aligns best with your project’s requirements.
The image below provides a visual representation of what the page looks like, although there are many more APIs available than what is visible in this screenshot.
For this particular project, we’ll be using the Google Sheets API. However, feel free to select any API that aligns with your specific project needs. Here’s how to enable an API:
Step 1: Click on the API you require, in our case, the ‘Google Sheets API’.
Step 2: Locate and click the ‘ENABLE’ button to activate the chosen API for your project.
3. Creating Credentials for Your Google Console Project
Now, let’s move on to creating credentials for your Google Console project.
Locate the “Credentials” tab in the left corner and click on it.
Before proceeding, you’ll need to configure the Consent Screen. Click on “CONFIGURE CONSENT SCREEN” to get started.
I have selected the option “External”. To select the option “Internal” you need to be a G suit user.
After completing the consent screen process outlined above, return to the ‘Credentials’ tab. Then, click on ‘CREATE CREDENTIALS’.
Additional Knowledge:
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 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., the 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 the credential by clicking on the credential name under the OAuth 2.0 Clients ID section.
If you’re eager to harness the full potential of Google API integration, I’m here to support your journey. Whether you have inquiries, need expert guidance, seek automated token generation solutions, or require customized consulting services, I’m ready to assist you. Let’s explore how I can help you swiftly achieve your objectives. Get in touch through the Contact Form, and let’s initiate a conversation about your specific needs.
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.
Go to https://developers.google.com/oauthplayground/
We need to ensure the access token is authorized for our app. 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.
- ‘https://www.googleapis.com/auth/drive.metadata.readonly‘
- ‘https://www.googleapis.com/auth/spreadsheets.readonly‘
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.
Focus:
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 Sheets 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 Tokens using the 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 set up Postman.
After setting up the 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”, and a popup asking for the 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 the “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”.
Focus:
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. To get code to generate access token along with refresh token. All these on an automated process get in touch with me through the Contact Form.
Congratulation! you have successfully set up Google Console for creating projects and apps. Hope I have made the entire process of creating a project in Google Console for APIs and generating access tokens and refresh tokens simple enough.
Hope I was able to solve the problem. If you like this article and think it was easy to understand do share it with your friends and connection. Thank you! See you soon.
If you have any questions or comments feel free to reach me at.
Checkout out my other API Integration and Coding Solution Guide