NodeJS环境下调用Google资源日历API获取会议室信息的技术咨询
Hey there! I’ve dealt with this exact issue before when building meeting room booking features, so let me walk you through the solutions:
First off, the Admin SDK Resource Calendars API is indeed supported in the official googleapis package—you just need to use the admin module instead of the regular calendar module. Here's how to set it up:
Step 1: Ensure you have the latest package
Make sure you're on the most recent version to avoid missing any methods:npm install googleapis@latestStep 2: Authenticate correctly
Since this is an Admin API, you'll need a service account with domain-wide delegation enabled, and you must impersonate a domain admin user (regular users don't have access to directory resources).Step 3: Code example to fetch resource calendars
Here's a working snippet to list all resource calendars in your domain:const { google } = require('googleapis'); async function fetchResourceCalendars() { // Initialize JWT auth with service account credentials const auth = new google.auth.JWT( process.env.SERVICE_ACCOUNT_EMAIL, null, process.env.SERVICE_ACCOUNT_PRIVATE_KEY, ['https://www.googleapis.com/auth/admin.directory.resource.calendar'], 'admin@your-domain.com' // Replace with your domain admin's email ); // Initialize the Admin SDK Directory client const adminService = google.admin({ version: 'directory_v1', auth }); try { const response = await adminService.resources.calendars.list({ customer: 'my_customer', // Use 'my_customer' to target your domain maxResults: 100, // Adjust based on your needs // Add optional filters like `query` if you need to search specific resources }); return response.data.items; // This contains your room/resource calendars } catch (error) { console.error('Failed to fetch resource calendars:', error.message); throw error; } } // Run the function fetchResourceCalendars();
If for some reason you prefer not to use the official client, you can call the REST endpoint directly using tools like axios or node-fetch. The key is getting a valid access token with the right scopes.
Example with axios:
const axios = require('axios'); const { google } = require('googleapis'); async function fetchResourceCalendarsREST() { const auth = new google.auth.JWT( process.env.SERVICE_ACCOUNT_EMAIL, null, process.env.SERVICE_ACCOUNT_PRIVATE_KEY, ['https://www.googleapis.com/auth/admin.directory.resource.calendar'], 'admin@your-domain.com' ); // Get an access token from the auth client const { token } = await auth.getAccessToken(); try { const response = await axios.get( 'https://admin.googleapis.com/admin/directory/v1/customer/my_customer/resources/calendars', { headers: { Authorization: `Bearer ${token}` }, params: { maxResults: 100 } } ); return response.data.items; } catch (error) { console.error('REST API Error:', error.response.data.error); throw error; } }
- Permissions: Ensure your service account has been granted the
https://www.googleapis.com/auth/admin.directory.resource.calendarscope in your Google Admin Console (under "Domain-wide Delegation"). - Impersonation: You must impersonate a domain admin user—regular users don't have permission to list domain resources.
内容的提问来源于stack exchange,提问作者Jeevan Patil

