Google API – Getting User ID for Google Chat from Google People API

Google API – Getting User ID for Google Chat from Google People API

How to Retrieve a Google User ID Using OAuth2 Playground

With OAuth2 Playground, you can securely interact with Google APIs without needing to build the OAuth2 authentication flow from scratch. Here’s a straightforward guide to help you retrieve the Google user ID in just a few steps:

Step 1: Access the Google OAuth2 Playground

Start by visiting the Google OAuth2 Playground. This is a helpful tool for developers who want to explore how Google’s OAuth2 system works and test API calls quickly.

Step 2: Select the People API v1 Scope

To retrieve a user’s ID, you’ll need to access the People API. In the OAuth2 Playground, under Step 1, select the appropriate API scope:

  • Click on “Select & Authorize APIs.”
  • Look for People API v1 and check it.

This API will allow you to access basic user information, including the Google ID.

image - quochung.cyou PTIT
Google API - Getting User ID for Google Chat from Google People API 14

Step 3: Authorize API Access

Once you’ve selected the API scope, click Authorize APIs. You’ll be redirected to a Google login page, where you’ll log in and grant the necessary permissions. This allows the OAuth2 Playground to act on behalf of the user and retrieve data from the People API.

image 1 - quochung.cyou PTIT
Google API - Getting User ID for Google Chat from Google People API 15

Step 4: Exchange Authorization Code for Tokens

After authorization, you’ll receive an authorization code in the Playground. In Step 2, click Exchange authorization code for tokens. This will provide you with an access token, which you’ll use to make authorized API requests.

Now that you have the access token, it’s time to make a request to the People API to get the user’s Google ID. Use the following CURL command in your terminal or directly in the OAuth2 Playground’s built-in HTTP request tool:

Java
curl --location 'https://people.googleapis.com/v1/people:searchDirectoryPeople?query=email_target&readMask=names&sources=DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer your-token-here'

Replace YOUR_ACCESS_TOKEN with the token you obtained in the previous step. This request will return a JSON object with the user’s profile data, including their Google ID.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply