Laravel Google Login, Calendar and Drive multiple API integration

Viewed 1217

I have a question maybe useful for most of Laravel developers. I want to allow user to connect to one or more of these APIs:

  • Google Login
  • Google Calendar
  • Google Drive

I have used Laravel Socialite so far, which provides clear connection to many social accounts. My issue is that through Socialite I have only one google Driver, while I need to address to different Google API from different buttons.

My .env data, connected to the config/services.php are:

GOOGLE_CLIENT_ID=1.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=XXX
GOOGLE_CALLBACK_URL=https://mysite/google/login_callback

GOOGLE_CALENDAR_CLIENT_ID=2.apps.googleusercontent.com
GOOGLE_CALENDAR_CLIENT_SECRET=YYY
GOOGLE_CALENDAR_CALLBACK_URL=https://mysite/google/calendar_callback

GOOGLE_DRIVE_CLIENT_ID=3.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=WWW
GOOGLE_DRIVE_CALLBACK_URL=https://mysite/google/drive_callback

Now, it is now clear to me how to manage multiple APIs from different buttons. Here some options:

  1. Use Socialite with unique credential entry point (google_login) from Google Console Cloud and in the callback function into the Controller I differentiate different cases (login, calendar, drive...)
  2. I create a different Google credential for each API into API section, I use Socialite only for login and I create/download separate package for Calendar, for Drive, etc...

So the main point is: multiple Google APIs in Laravel need to be applied with unique API credential and at Controller level I address to relative code using scopes or I have to create a separate API credential of each API, make the relevant .env keys and the manage all separately.

I hope to have been not to much blur. thanks!

1 Answers

With the socialite package, you can only do authentication. If you are developing for Calendar and Drive, I would suggest to use google client library

 $client = new \Google_Client();
 $client->setAccessToken($accessToken);
 $client->setSubject($email);
 $clien->addScope(['{scopes}']);
 $client->authorize();

 $client->get('https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events');

Please also refer this document: Event apis Drive Apis

Related