Using passport and connecting to 3rd party Google Calendar API using REST connector

431 views
Skip to first unread message

baldeagle

unread,
Feb 16, 2016, 2:13:43 AM2/16/16
to LoopbackJS
Hi, I new to loopback (and node) and would love assistance connecting to the Google Calendar API.

I've been able to set up user login using loopback-component-passport and would now like to connect to Google Calendar as a 3rd party API. I understand I must use a REST connector for this.

I've created a Calendar model that mirrors that of the Google Calendar resource and a datasource like this:

  "googlecalendar": {
    "name": "Google Calendar API",
    "connector": "rest",
    "operations": [
      {
        "template": {
          "method": "GET",
          "headers": {
              "accepts": "application/json",
              "content-type": "application/json"
          },
          "strictSSL": false,
          "query": {
              "key": "{apikey}"
          }
        }
      }
    ]
  }

I've made googlecalendar the dataSource for the Calendar model.

Now:
  1. Where do I set up a REST end point so I can PUT /calendars - do I even need to or is it already taken care of?
  2. How do I append ?key={apikey} with the apikey from loopback-component-passport?
  3. And how do I add a scope of https://www.googleapis.com/auth/calendar when the user originally registers/logs in?
These are the things I'm referencing but have become confused and not sure what to do next:

Any corrections, additions or pointers would be greatly appreciated.

Thank you.

baldeagle

unread,
Feb 16, 2016, 2:47:48 AM2/16/16
to LoopbackJS
Without doing anything else, when I use the API Explorer to PUT /Calendars with data { "summary": "test" } I get the following error:

Cannot call Calendar.upsert(). The upsert method has not been setup. The PersistedModel has not been correctly attached to a DataSource!

Is PersistedModel the incorrect base for the Calendar model?

baldeagle

unread,
Feb 16, 2016, 3:05:08 AM2/16/16
to LoopbackJS
This is my model (so far):

{
  "name": "Calendar",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": false
  },
  "properties": {
    "summary": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "location": {
      "type": "string"
    },
    "timeZone": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}


And then in model-config.json:

"Calendar": {
  "dataSource": "googlecalendar",
  "public": true
}

baldeagle

unread,
Feb 16, 2016, 4:55:42 AM2/16/16
to LoopbackJS
I feel like I'm completely on the wrong track here :-( and would like to explain the simplest use cases so that I may receive some valuable guidance:
  1. Loopback (the server app) will track users and calendars. Users register and login granting https://www.googleapis.com/auth/calendar scope.
  2. With the user logged in, their list of calendars is fetched by loopback using GET https://www.googleapis.com/calendar/v3/users/me/calendarList and returned to the frontend app (here using Ampersand).
  3. In Ampersand, the user selects which calendar they wish to use and the calendar's ID is stored by loopback in the Calendar model with a reference to the calendar in the User's model (these are all stored in mongoDB).
  4. Then, for any logged in user, Ampersand will fetch from loopback a list of users to display.
  5. When a user is selected, loopback retrieves the related calendar ID, fetches the calendars free time using POST https://www.googleapis.com/calendar/v3/freeBusy and returns items to Ampersand.
  6. The logged in user is then able to create an event for the calendar with themselves as an attendee using POST https://www.googleapis.com/calendar/v3/calendars/calendarId/events.
These are what I believe to be the loopback endpoints:
  • GET /auth/google
  • GET /Users/{id}/calendarList
  • POST /Calendars
  • GET /Users
  • GET /Calendars/{id}/freeBusy
  • POST /Calendars/{id}/events
  • GET /Calendars/{id}/events{fk}
So, does this mean the Calendar model should rather be connected to the mongoDB datasource? And then use remote hooks with app.dataSources.googlecalendar?

Aurélie Violette

unread,
Feb 16, 2016, 11:38:19 AM2/16/16
to LoopbackJS
If you want to discuss, I implement something similar in my project, but with a service rather than a datasource to handle google api connection (may be i am wrong, and your approach is better, but it's working)
You can have a view here in Tournament model (but there is no readme yet, so feel free to ask more information)
I also make a quick exemple of connecting to google apis here (not totally related to loopback, only use it for demo purpose, but you can re-use some part)

Maybe it is not the answer you ask, but if it can make what you want to do possible, it will be fine
 

Aurélie Violette

unread,
Feb 16, 2016, 11:39:51 AM2/16/16
to LoopbackJS
Also, if somebody can make it works with datasources, I am interested !
Reply all
Reply to author
Forward
0 new messages