Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use gapi with service account? #854

Open
clariceabreu opened this issue Feb 25, 2023 · 3 comments
Open

How to use gapi with service account? #854

clariceabreu opened this issue Feb 25, 2023 · 3 comments

Comments

@clariceabreu
Copy link

I'm trying to use Google Service account (https://developers.google.com/identity/protocols/oauth2/service-account#java) in my app now that gapi.client.init doesn't work anymore but I don't know how to do it with this library.

My app is a website for a hotel and I want to show a calendar with the bookings and availability. All the booking events are on the Google Calendar account of the hotel owner so I just want to fetch the events to display them to the user.

I have already created a Service Account credential here: https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?supportedpurview=project

@boorge
Copy link

boorge commented Oct 17, 2023

To access Google Calendar events using a service account, you'll need to use the Google API Client Library. Below are the steps to achieve this in JavaScript:

Set Up Your Google Cloud Project:
Make sure you have a Google Cloud project and that the Calendar API is enabled.

Create a Service Account:
You've already created a service account, which is great. Ensure that you download the JSON key file associated with this service account. This file contains the credentials you'll use to authenticate.
Install the Google API Client Library:

Install the googleapis library using npm:

npm install googleapis

Write JavaScript Code:
Use the following code as a reference to fetch events from the Google Calendar:

const { google } = require('googleapis');
const key = require('./path/to/your/service-account-key.json'); // Replace with the path to your JSON key file

const jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/calendar.readonly'], // Adjust scopes as needed
);

const calendar = google.calendar('v3');

jwtClient.authorize((err, tokens) => {
  if (err) {
    console.error('Error authorizing service account:', err);
    return;
  }

  calendar.events.list({
    auth: jwtClient,
    calendarId: 'primary', // Use the appropriate calendar ID
  }, (err, response) => {
    if (err) {
      console.error('Error fetching events:', err);
      return;
    }

    const events = response.data.items;
    console.log('Events:', events);
  });
});

Adjust the calendarId to match the Google Calendar you want to access. The calendar.readonly scope is used for fetching events.

Run Your Application:
Execute your JavaScript application, and it should fetch events from the specified Google Calendar.
Important Notes:

Service Account Permissions:
Ensure that the service account has the necessary permissions to access the target Google Calendar. Share the calendar with the service account email address.

Calendar ID:
Make sure to replace 'primary' with the actual Calendar ID you want to access.
Service Account Key Path:

Replace ./path/to/your/service-account-key.json with the actual path to your downloaded service account JSON key file.

By following these steps, you should be able to access Google Calendar events using a service account in your hotel website application.

@akshat-bhansali
Copy link

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

this does not work in my reactjs application. it throws a lot of errors.
I want to use drive for uploading files from frontend.I know it will not be secure but its okay.

@dvanderb
Copy link
Collaborator

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

this does not work in my reactjs application. it throws a lot of errors. I want to use drive for uploading files from frontend.I know it will not be secure but its okay.

The issue is you are trying to do something that is unsupported. Service accounts are supposed to be used for server to server communication(backends.. in this case 'googleapis' is a nodejs module). Frontend applications can use gapi.client but then they need to use api_key/client_id and OAuth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants