Where to find credentials.json for Google API client?

Roymunson picture Roymunson · Oct 19, 2019 · Viewed 19.9k times · Source

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.

Answer

Tanaike picture Tanaike · Oct 20, 2019
  • 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.