Google Calendar "The request is missing a valid API key" when using OAuth

Viewed 3560

I have an OAuth token stored in a database and I am trying to use it to list the calendar events for the calendar. I tried a proof of concept in another language and through that I have verified that my clientID, clientSecret, redirectURL, and refreshTokens all work correctly, but for some reason in Node.js I keep getting a The request is missing a valid API key. error even though the documentation clearly states that I don't need to use an API key if I use an OAuth token.

I am using "googleapis": "^66.0.0"

Any ideas of what I am doing wrong?

The simple as possible code:

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

const client = new google.auth.OAuth2(clientID, clientSecret, redirectURL);
client.forceRefreshOnFailure = true;

client.setCredentials({
  refresh_token: '<secret>',
});

const service = google.calendar({ version: 'v3', client });
service.events.list({
  calendarId: '<secret>',
  singleEvents: true,
}, (res, err) => {
  console.log(res)
});

The error message:

config: {
  client: OAuth2Client {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    transporter: DefaultTransporter {},
    credentials: {
      refresh_token: '<secret>'
    },
    certificateCache: {},
    certificateExpiry: null,
    certificateCacheFormat: 'PEM',
    refreshTokenPromises: Map(0) {},
    _clientId: '<secret>',
    _clientSecret: '<secret>',
    redirectUri: '<secret>',
    eagerRefreshThresholdMillis: 300000,
    forceRefreshOnFailure: true,
    [Symbol(kCapture)]: false
  },
  url: 'https://www.googleapis.com/calendar/v3/calendars/<secret>/events?singleEvents=true',
  method: 'GET',
  userAgentDirectives: [
    {
      product: 'google-api-nodejs-client',
      version: '4.4.3',
      comment: 'gzip'
    }
  ],
  paramsSerializer: [Function (anonymous)],
  headers: {
    'x-goog-api-client': 'gdcl/4.4.3 gl-node/15.5.0 auth/6.1.3',
    'Accept-Encoding': 'gzip',
    'User-Agent': 'google-api-nodejs-client/4.4.3 (gzip)',
    Accept: 'application/json'
  },
  params: { singleEvents: true },
  validateStatus: [Function (anonymous)],
  retry: true,
  responseType: 'json',
  retryConfig: {
    currentRetryAttempt: 0,
    retry: 3,
    httpMethodsToRetry: [ 'GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE' ],
    noResponseRetries: 2,
    statusCodesToRetry: [ [ 100, 199 ], [ 429, 429 ], [ 500, 599 ] ]
  }
},
code: 403,
errors: [
  {
    message: 'The request is missing a valid API key.',
    domain: 'global',
    reason: 'forbidden'
  }
]
1 Answers

Modification points:

  • In this case, please use the key of auth at google.calendar() like google.calendar({ version: "v3", auth: client }). I think that this is the reason of your error message.
  • And also, about }, (res, err) => { of response values, the 1st argument is the error message. The result value is 2nd argument.

When above points are reflected to your script, it becomes as follows.

Modified script:

const service = google.calendar({ version: "v3", auth: client });  // Modified
service.events.list(
  {
    calendarId: '<secret>',
    singleEvents: true,
  },
  (err, res) => {  // Modified
    if (err) {
      console.log(err);
      return;
    }
    console.log(res.data);
  }
);

Note:

  • In this modification, it supposes that the access token retrieved from your refresh token can be used for retrieving the event list from your calendar. Please be careful this.

Reference:

Related