26

The Google Calender Node.js example requires a file called "credentials.json": https://developers.google.com/calendar/quickstart/nodejs

Relevant code:

// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  // Authorize a client with credentials, then call the Google Calendar API.
  authorize(JSON.parse(content), listEvents);
});
function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getAccessToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

I don't know where to find this file. The Google API console offers a "download JSON" option, but the file is not in the right format and it is missing the redirect_uris field.

1 Answer 1

27
  • You want to retrieve credentials.json file for using Google API with Quickstart of Node.js.

If my understanding is correct, how about this answer? Please think of this as just one of several answers.

In this answer, it supposes that you clicked "Enable the Google Calendar API" button.

Flow:

  1. When you click "Enable the Google Calendar API" button, the following screen can be seen. Here, please click "API console".

    • enter image description here
  2. When you click "API console", the following screen can be seen. Here, please click "Credentials".

    • enter image description here
  3. When you click "Credentials", the following screen can be seen. Here, please click the download button. By this, you can retrieve the JSON file. At this time, please rename the file to credentials.json, and put it to the directory with the path for using at Quickstart of Node.js.

    • enter image description here

Note:

  • If you are required to create new Cloud Platform Project, this information might be useful.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

3
  • 1
    How can this be accepted answer ? The downloaded file still miss the redirect_url as shown below: Commented Jul 4, 2021 at 20:02
  • How can this be the acceptable answer ? The download JSON file still miss redirect_url as OP specified, also show here: calendar\quickstart\index.js:45 client_id, client_secret, redirect_uris[0]); Commented Jul 4, 2021 at 20:02
  • ADD REDIRECT_URL: (1) Go to Credentials (2) Under OAuth 2.0 Client IDs, click on the project name or the edit pencil logo (3) Towards the end of the page, find Authorized redirect URIs then click the ADD URI button (4) Save and DOWNLOAD THE .json file AGAIN Commented Nov 23, 2021 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.