How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (2024)

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (1)

Gbadebo Bello

· 12 mins

Authorization is a fundamental part of working with an API. There are many standards that define how it is done, but the Open Authorization 2.0 standard—referred to as OAuth 2.0 for short—is the most popular and widely used.

OAuth 2.0 plays an important role in API data security. It provides a standardized and secure protocol for authorization between APIs and third-party applications that doesn’t require users to share credentials. It also allows an application to get user-consented access to specific data without requesting any confidential data (such as passwords) from the user. Large-scale and enterprise organizations use OAuth 2.0 as a primary method for authorizing access to their users’ data, and it has grown over the years to become an industry standard.

In this post, we’ll show how you can use Postman to access a Google API using OAuth 2.0. But first, we’ll review access and refresh tokens—and explain how OAuth 2.0 works.

Related: What is OAuth 2.0?

Access and refresh tokens

An access token is an authorization string that is issued to a third-party application. These tokens represent specific scopes that have been granted by the user or resource owner and are often short-lived. Access tokens can be stored in different formats, the most common being the JWT (JSON Web Tokens) format. This format ensures that the token can also contain some encrypted data, which can be securely retrieved before the token expires.

Because access tokens are often short-lived, there needs to be a way to generate a new token when the previous token is no longer valid or has expired. Refresh tokens are used to obtain new access tokens and often have a longer lifespan than access tokens. However, not all providers issue refresh tokens; the availability of a refresh token is determined by the API provider.

The following roles exist within the OAuth 2.0 specification:

  • Resource owner: This is the user who is granting third-party access to their data.
  • Resource server: This is the server that is hosting the protected resources. It is responsible for accepting and responding to requests to access protected resources using an access token.
  • Client: The client is the third-party application that is requesting authorization from the resource owner. When the resource owner grants access, the client gets the access token that can be used to request the resources within the granted scope.
  • Authorization server: The authorization server issues access tokens to the client after the resource owner successfully authorizes the request. This process involves two steps. First, the client is issued a code on authorization, which is then used to request the access token from an access token URL provided by the authorization server.

How does OAuth 2.0 work?

Let’s take a look at the step-by-step process involved in implementing the OAuth 2.0 authorization method:

  1. The client gets a client ID and a client secret from the authorization server, which it uses to identify itself when requesting an access token.
  2. The client requests authorization from the user (i.e., the resource owner) to access their resources on the resource server.
  3. The user grants authorization to the client through the authorization server by logging in with their credentials. These credentials are not shared with the client. Instead, an authorization code is generated and shared with the client.
  4. The client uses this authorization code to request the access token from an endpoint provided by the authorization server.
  5. The authorization server generates an access token, which the client can use to access the user’s resources on the resource server.
  6. The client sends the access token to the resource server to request access to the user’s resources.
  7. The resource server validates the access token with the authorization server and, if valid, grants the client access to the user’s resources.

This process ensures that the user’s credentials are not shared with the third-party application and that the user has control over which resources the application can access.

Using Postman to access a Google API

The Postman API client lets you work with different types of API authorization methods, including OAuth 2.0. It makes it easy to generate and exchange an authorization code for an access token—and it even provides an option that automatically refreshes your authorization tokens when they expire, assuming a refresh token was returned from the authorization server.

In this section, we’ll show you how to use Postman to access a Google API with OAuth 2.0 as part of your API development and testing workflow. We’ll be using the Google Sheets API for our example, but you can use any other Google API and follow the same flow that’s documented here.

Step 1: Create and set up a new project

Note: If you already have a project set up in Google, you can skip this step.

Navigate to the Google Cloud Console and select the dropdown in the top navigation menu. Then, click on the New Project button:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (2)

Give your project a name, and select a location and an organization:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (3)

The notification modal will pop up. Click the Select Project button for the newly created project:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (4)

Next, in the side navigation menu, hover over APIs & Services and select Library:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (5)

From the library, search for “Google Sheets API” and enable it:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (6)

Step 2: Get your credentials

Once you’ve enabled the API you intend to work with, click the Create Credentials button at the top right of the API dashboard:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (7)

The Google Sheets API should be auto-selected under the Credentials Type section, but you can choose any other API you’d like to use. Select User data as the data you want to access and click Next:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (8)

Now, give your app a name, select a support email address, and upload a logo for your app, if you have one. Click Save and continue. Then, click the Add or Remove Scopes button and search for “Sheets.” Select the read-only option and click Update:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (9)

Click Save and continue. Next, under OAuth Client ID, select Web Application as your application type and give the client a name:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (10)

Under Authorized redirect URIs, click the Add URI button. The URI you add will depend on what Postman environment you’re testing from. If you’re testing from Postman’s web application, add the following redirect URI: https://oauth.pstmn.io/v1/browser-callback. If you’re testing from the Postman desktop application, add this URI: https://oauth.pstmn.io/v1/callback. If you switch between the web and desktop application during your development workflow, you should add both URIs:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (11)

Copy your client ID and click the Download button to download all of your credentials:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (12)

Once your download is complete, navigate to the Credentials tab and click on the newly added credential:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (13)

You will see your client credentials (i.e., your client ID and client secret) on the right-hand side of your dashboard:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (14)

Step 3: Authorize with Postman

Now that you have your client ID and client secret, you can use Postman to authorize the Google Sheets API in your automated API testing or API development workflow. Create a Postman Collection and add a new request. Then, navigate to https://sheets.new to automatically create a new Google Sheet on your Google account. Populate this Sheet with random data and give it a name.

You can access the ID of this Sheet from the URL navigation bar. The ID typically appears after /spreadsheets/d/, as shown in the screenshot below. Copy this ID to your clipboard:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (15)

In Postman, add the base URL https://sheets.googleapis.com/v4/spreadsheets in the URL tab and include the Sheet ID as a path variable:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (16)

This request is intended to fetch the data of your Google Sheet in JSON. However, if you click Send, you will get a “401 Unauthorized” error response, as shown above. This happens because we have not provided the right authentication credentials to make this request. Postman is a third-party application (i.e., a client) trying to fetch Google Sheets data from your Google account (i.e., a resource server). As the resource owner, you need to first authorize Postman and grant it the necessary scopes to fetch this data. To do so, navigate to the Authorization tab in your Postman account and select OAuth 2.0 as your authorization type:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (17)

Scroll down to the Configure New Token section and fill in the following details:

  • Grant Type: Select “Authorization Code” to let Postman know that the resource server will be providing an authorization code that it will use to get an access token.
  • Callback URL: This is the redirect URL you specified in your Google Cloud Console. It is automatically set for you and will vary depending on whether you’re using the Postman desktop app or the web client.
  • Auth URL: This is the authorization server endpoint. It presents the user with a UI to authorize the client (so far, the user is logged in). The requested scopes are displayed, and the user can choose to accept or decline access to their data. When the user accepts, it navigates to the callback URL with an authorization code included as a query parameter. This callback URL navigates back to Postman, and Postman will then use the authorization code to fetch the access token from the resource server. Set this field to https://accounts.google.com/o/oauth2/v2/auth.
  • Access Token URL: This is the interface exposed by the resource server for exchanging an authorization code with an access token. Set this field to https://oauth2.googleapis.com/token.
  • Client ID: This is the client ID that was generated in your Google Cloud Console. Store this ID in a variable to keep sensitive data secure.
  • Client Secret: This is the client secret that was generated in your Google Cloud Console. Store the secret in a variable to keep sensitive data secure.
  • Scope: These are the scopes to which you want to request access from the client. Multiple scopes are separated by a space. Set this field to https://spreadsheets.google.com/feeds/.

Note: Google does not return a refresh token by default. In order to receive a refresh token on authorization from a Google API, you need to add an extra query parameter to your auth URL. Modify your auth URL to https://accounts.google.com/o/oauth2/v2/auth?access_type=offline, which includes a query parameter of access_type set to offline. The default state is online, and you need to explicitly set it to offline.

Once you’ve entered this data, click the Get New Access Token button:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (18)

When you click the button, Postman will automatically open a new tab in your web browser:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (19)

Google will then prompt you to select an account to authenticate with. Alternatively, you can sign in to another account that you would like to use:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (20)

After you select an account, Google will display the scopes requested and prompt you to either allow or cancel this request:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (21)

Selecting Allow will generate an authorization code and redirect you back to the callback URL with the authorization code included. The callback URL will prompt you to navigate back to Postman to either complete or cancel the authorization process:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (22)

Once you accept this prompt, you will see a success modal and be navigated back to Postman:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (23)

Once you’re back in Postman, you will see a modal that looks like the one in the screenshot below. This modal will include your newly generated access token and other relevant metadata. This access token was generated by providing your authorization code to the resource server. Click on the Use Token button to automatically include it in the Token field. Any token specified in the Token field will automatically be included in your request headers and will therefore authenticate you to make the necessary requests:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (24)

If you re-run the same request in Postman by clicking the Send button, you will notice that it now returns the Sheet’s data in JSON, indicating that you are now authorized to request this resource:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (25)

Step 4: Managing tokens

You can generate new tokens after expiration by following the same process we discussed above, but you won’t have to go through every step again. If you have a token added, you can manage all your tokens.

In the Authorization tab, click on the token list option and select Manage Tokens:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (26)

Here, you can see a list of added tokens, alongside their validity. You can choose to delete any tokens that you no longer need:

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (27)

Step 5: Revoking access

You can navigate to https://myaccount.google.com/permissions to see a list of all third-party applications that have access to your account. You can then choose to revoke authorization access to any of these applications by clicking Remove access. In our case, the application to look for is called “Postman x Google.”

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (28)

Additional resources

Related: Use the Authorization Methods Template

To follow along with this tutorial, watch the video and reference the documentation at Postman Quickstarts. The collection created in this tutorial can be found here.

Technical review by Arlemi Turpault

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog (2024)

FAQs

How to Access Google APIs Using OAuth 2.0 in Postman | Postman Blog? ›

To get started with OAuth 2.0 in Postman, you need to first configure the authorization settings for your API request. This can be done by selecting the "OAuth 2.0" option from the authorization type dropdown menu and filling in the required details, such as the authorization URL and access token URL.

How to access Google API in Postman? ›

Using Postman to access a Google API
  1. Give your project a name, and select a location and an organization:
  2. The notification modal will pop up. ...
  3. Next, in the side navigation menu, hover over APIs & Services and select Library:
  4. From the library, search for “Google Sheets API” and enable it:
  5. Step 2: Get your credentials.
Mar 13, 2023

How to use OAuth 2.0 in Postman? ›

To get started with OAuth 2.0 in Postman, you need to first configure the authorization settings for your API request. This can be done by selecting the "OAuth 2.0" option from the authorization type dropdown menu and filling in the required details, such as the authorization URL and access token URL.

How do I give access to Google API? ›

Granting access
  1. In the Google Cloud console, go to the Endpoints > Services page for your project. ...
  2. If you have more than one API, click the name of the API.
  3. If the Permissions side panel isn't open, click addPermissions.
  4. In the Add members box, enter the email address of a user, service account, or Google Group.

How to authenticate with Google Service account credentials using Postman and https? ›

Using oauth2l CLI. Configuring Postman with OAuth 2 and User Credentials. Configuring Postman to use a pre-request script. This will auto-generate Google Access and ID tokens from a Service Account key and save it in Postman.

How do I access Google APIs? ›

To enable an API for your project:
  1. Go to the API Console.
  2. From the projects list, select a project or create a new one.
  3. If the APIs & services page isn't already open, open the console left side menu and select APIs & services, and then select Library.
  4. Click the API you want to enable. ...
  5. Click ENABLE.

How to authenticate API using OAuth? ›

At a high level, you follow five steps:
  1. Obtain OAuth 2.0 credentials from the Google API Console. ...
  2. Obtain an access token from the Google Authorization Server. ...
  3. Examine scopes of access granted by the user. ...
  4. Send the access token to an API. ...
  5. Refresh the access token, if necessary.

What is the difference between OAuth 1.0 and OAuth 2.0 in Postman? ›

OAuth 1.0 only handled web workflows, but OAuth 2.0 considers non-web clients as well. Better separation of duties. Handling resource requests and handling user authorization can be decoupled in OAuth 2.0. Basic signature workflow.

How do I unlock Google API? ›

In the Google Cloud console, go to APIs & services for your project. On the Library page, click Private APIs. If you don't see the API listed, that means you haven't been granted access to enable the API. Click the API you want to enable.

How do I get a Google API code? ›

  1. In the Google Cloud console, go to the Credentials page: Go to Credentials.
  2. Click Create credentials, then select API key from the menu. The API key created dialog displays the string for your newly created key.

What is Google API access token? ›

Access tokens are opaque tokens that conform to the OAuth 2.0 framework. They contain authorization information, but not identity information. They are used to authenticate and provide authorization information to Google APIs.

How to call oauth2 API in Postman? ›

Setting up OAuth 2.0 in Postman
  1. Open the Collections tab on the left side of the screen.
  2. Open your collection or create a new one.
  3. On the Authorization tab, select OAuth 2.0 from the Type drop-down.
  4. Select Request Headers from the Add auth data to drop-down.
  5. Click the Authorize using browser checkbox.

How to get Google OAuth2 access token in Postman? ›

Using OAuth 2.0
  1. In the Authorization tab for a collection or request, select OAuth 2.0 from the Auth Type dropdown list. ...
  2. To request an access token, fill out the fields in the Configure New Token section, and select Get New Access Token.

How do I automatically authenticate in Postman? ›

You can pass auth details along with any request you send in Postman. Auth data can be included in the header, body, or as parameters of a request. If you enter your auth details in the Authorization tab of a request, Postman will automatically populate the relevant parts of the request for your chosen auth type.

How do I access API from Postman? ›

Send an API request

When you're ready, open the Postman desktop app and send your first API request. Select + in the workbench to open a new tab. Enter postman-echo.com/get for the request URL. Select Send.

How do I open API in Postman? ›

In Postman, select Import in the sidebar. Select an API definition file, enter a link to the API, or paste your raw text. You can choose to import the definition as a collection or as an API along with a collection. Select View Import Settings for more configuration options.

How to use Google Calendar API in Postman? ›

Get Calendar
  1. alt. json. Data format for the response.
  2. fields. amet in. Selector specifying which fields to include in a partial response.
  3. key. amet in. API key. ...
  4. oauth_token. amet in. OAuth 2.0 token for the current user.
  5. prettyPrint. true. Returns response with indentations and line breaks.
  6. quotaUser. amet in. ...
  7. userIp. amet in.

How do I access https API in Postman? ›

In the address bar at the top, select the request method and enter the API address with HTTPS, for example https://postman-echo.com/get. 3. Click the "Send" button and wait for the server response to get the corresponding result.

Top Articles
Red Flag Due Diligence - Conpair
The Architecture of Open Source Applications (Volume 2)Moodle
Po Box 7250 Sioux Falls Sd
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Tesla Supercharger La Crosse Photos
Kokichi's Day At The Zoo
Kansas Craigslist Free Stuff
Shorthand: The Write Way to Speed Up Communication
Obituary (Binghamton Press & Sun-Bulletin): Tully Area Historical Society
Best Theia Builds (Talent | Skill Order | Pairing + Pets) In Call of Dragons - AllClash
Acbl Homeport
123 Movies Babylon
Mercy MyPay (Online Pay Stubs) / mercy-mypay-online-pay-stubs.pdf / PDF4PRO
Springfield Mo Craiglist
Love In The Air Ep 9 Eng Sub Dailymotion
Midlife Crisis F95Zone
065106619
Craftology East Peoria Il
Eva Mastromatteo Erie Pa
Palm Coast Permits Online
Bj Alex Mangabuddy
Best Nail Salons Open Near Me
What Is The Lineup For Nascar Race Today
Jordan Poyer Wiki
Prot Pally Wrath Pre Patch
Walmart Pharmacy Near Me Open
Beaufort 72 Hour
Bleacher Report Philadelphia Flyers
4Oxfun
JVID Rina sauce set1
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Ou Football Brainiacs
Miles City Montana Craigslist
Hrconnect Kp Login
Angel Haynes Dropbox
Publix Christmas Dinner 2022
Mini-Mental State Examination (MMSE) – Strokengine
Motor Mounts
Kamzz Llc
4083519708
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
Kutty Movie Net
6576771660
30 Years Of Adonis Eng Sub
Port Huron Newspaper
Devotion Showtimes Near Showplace Icon At Valley Fair
Headlining Hip Hopper Crossword Clue
552 Bus Schedule To Atlantic City
Germany’s intensely private and immensely wealthy Reimann family
Roller Znen ZN50QT-E
Sam's Club Fountain Valley Gas Prices
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6544

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.