node google client api to get user profile with already fetched token

const { google } = require('googleapis');

async function getUserProfile(accessToken) {
  const oauth2Client = new google.auth.OAuth2();
  oauth2Client.setCredentials({ access_token: accessToken });

  const people = google.people({ version: 'v1', auth: oauth2Client });

  try {
    const profile = await people.people.get({
      resourceName: 'people/me',
      personFields: 'names,emailAddresses',
    });

    console.log('User Profile:', profile.data);
    return profile.data;
  } catch (error) {
    console.error('Error fetching user profile:', error.message);
    throw error;
  }
}

// Usage example:
const accessToken = 'YOUR_ACCESS_TOKEN';
getUserProfile(accessToken);