Manipulate Google Calendar data for Meteor app users with a Google account?

How can I manipulate Google Calendar data for users registered in my Meteor app with a Google account?

For example, I want to insert events or at least send invitations to an event. My users are logging into my app with a google account, but I want to insert events even if they are logged out. Below is the server code I have so far, but it is giving me "insufficient permission" errors.

  if (user && user.services && user.services.google && 
        user.services.google.accessToken) {
    var startTimeUTC = moment.utc(event.startTime, "YYYY-MM-DD HH:mm:ss").format();
    var endTimeUTC = moment.utc(event.endTime, "YYYY-MM-DD HH:mm:ss").format();
    console.log("POSTINGGCAL");
    if (moment.utc().unix() < moment.utc(event.endTime).unix()) {
      console.log("ENTERGCAL");
      var id = Meteor.http.post("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
        'headers' : { 
          'Authorization': "Bearer " + user.services.google.accessToken,
          'Content-Type': 'application/json' 
        },
        'data': {
          "summary": event.name,
          "location": event.location,
          "start": {
            "dateTime": startTimeUTC,
          },
          "end": {
            "dateTime": endTimeUTC,
          },
          "attendees": [
            {
              "email": user.email,
            },
          ]          
        }
      });
    }//end if moment
  }//end if user

      

Errors:

W20141207-16:53:31.883(7)? (STDERR) 
W20141207-16:53:31.883(7)? (STDERR) /Users/.meteor/packages/meteor-tool/.1.0.35.lzatbc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/lib/node_modules/fibers/future.js:206
W20141207-16:53:31.884(7)? (STDERR)                         throw(ex);
W20141207-16:53:31.884(7)? (STDERR)                               ^
W20141207-16:53:31.909(7)? (STDERR) Error: failed [403] {  "error": {   "errors": [    {     "domain": "global",     "reason": "insufficientPermissions",     "message": "Insufficient Permission"    }   ],   "code": 403,   "message": "Insufficient Permission"  } } 
W20141207-16:53:31.909(7)? (STDERR)     at Object.Future.wait (/Users/.meteor/packages/meteor-tool/.1.0.35.lzatbc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/lib/node_modules/fibers/future.js:326:15)
W20141207-16:53:31.910(7)? (STDERR)     at Object.call (packages/meteor/helpers.js:118)
W20141207-16:53:31.910(7)? (STDERR)     at Object.HTTP.post (packages/http/httpcall_common.js:56)
W20141207-16:53:31.910(7)? (STDERR)     at scheduleIt (app/server/scheduler.js:243:28)
W20141207-16:53:31.910(7)? (STDERR)     at app/server/startup.js:16:8
W20141207-16:53:31.911(7)? (STDERR)     at _.extend.forEach (packages/mongo/mongo_driver.js:894)
W20141207-16:53:31.911(7)? (STDERR)     at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:741)
W20141207-16:53:31.911(7)? (STDERR)     at app/server/startup.js:14:20
W20141207-16:53:31.912(7)? (STDERR)     at _.extend.forEach (packages/mongo/mongo_driver.js:894)
W20141207-16:53:31.912(7)? (STDERR)     at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:741)
W20141207-16:53:31.912(7)? (STDERR)     - - - - -
W20141207-16:53:31.913(7)? (STDERR)     at makeErrorByStatus (packages/http/httpcall_common.js:12)
W20141207-16:53:31.913(7)? (STDERR)     at Request._callback (packages/http/httpcall_server.js:95)
W20141207-16:53:31.913(7)? (STDERR)     at Request.self.callback (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:122:22)
W20141207-16:53:31.913(7)? (STDERR)     at Request.emit (events.js:98:17)
W20141207-16:53:31.914(7)? (STDERR)     at Request.<anonymous> (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:888:14)
W20141207-16:53:31.914(7)? (STDERR)     at Request.emit (events.js:117:20)
W20141207-16:53:31.914(7)? (STDERR)     at IncomingMessage.<anonymous> (/Users/.meteor/packages/http/.1.0.8.l3xm0k++os+web.browser+web.cordova/npm/node_modules/request/request.js:839:12)
W20141207-16:53:31.914(7)? (STDERR)     at IncomingMessage.emit (events.js:117:20)
W20141207-16:53:31.915(7)? (STDERR)     at _stream_readable.js:929:16
W20141207-16:53:31.915(7)? (STDERR)     at process._tickCallback (node.js:419:13)
=> Exited with code: 8

      

EDIT: I added the following on the client, and while I don't get any more errors, the calendar event is not showing in my test google account. Any ideas?

Accounts.ui.config({
    requestPermissions: {
        google: 
        ['https://www.googleapis.com/auth/calendar',
        'https://www.googleapis.com/auth/calendar.readonly',
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/tasks'],
//      forceApprovalPrompt: {google: true}
    }, 
    forceApprovalPrompt: {google: true},
    requestOfflineToken: {google: true},
    passwordSignupFields: 'EMAIL_ONLY',
//      extraSignupFields: []
});

      

Following the Meteor docs that don't help as I get this error: Cleanup Error: Accounts.ui.config: Invalid Key: forceApprovalPrompt

+2


source to share


1 answer


Look at your error on Google API:

insufficientPermissions

      

This means that the token you used was reached by asking the user to just sign in without setting up calendar access for scope permissions.

So when you allow a user to login and you are using the package Meteor.loginWithGoogle

or accounts-ui

, you need to request the Calendar API . Volume:



Meteor.loginWithGoogle({requestPermissions: ['https://www.googleapis.com/auth/calendar'], forceApprovalPrompt: true})

      

or for use with accounts-ui

Accounts.ui.config({requestPermissions:{google:['https://www.googleapis.com/auth/calendar']}, forceApprovalPrompt: {google: true}})

      

0


source







All Articles