Unauthorized access to Google Calendar API post request

后端 未结 1 839
情书的邮戳
情书的邮戳 2021-01-03 16:44

I\'m trying to use Google calendar REST API within Meteor I can use any GET method without any problem, but when I try to create an event in a calendar I get an Unauthorized

相关标签:
1条回答
  • 2021-01-03 17:45

    After quite a good while of trying got it right..

    Added the following file to my client folder

     Accounts.ui.config({requestPermissions: {google: 
      ['https://www.googleapis.com/auth/calendar',
      'https://www.googleapis.com/auth/userinfo.profile',
      'https://www.googleapis.com/auth/tasks']}}, requestOfflineToken: {google: true})
    
    gCal = 
      insertEvent: (cliente, poblacion, texto, fecha)->
      #to-do calendar devuelve un Event Object que incluye un ID
      # si incluimos este id como campo en la alerta podremos despues
      # eliminar el evento en el calendario directamente desde la app
        url = "https://www.googleapis.com/calendar/v3/calendars/primary/events"
        event=  {
          summary: cliente
          location: poblacion
          description: texto
          start:
            "date": fecha
          end:
            "date": fecha
          }
        evento = JSON.stringify event
        console.log evento
        Auth = 'Bearer ' + Meteor.user().services.google.accessToken
        Meteor.http.post url, {
          params: {key: 'INSERT-YOUR-API-KEY-HERE'},
          data: event,
          headers: {'Authorization': Auth }
          }, 
          (err, result)->
            console.log result
            return result.id
    

    if you logged in thru {{loginButtons}} and then call insertEvent it works like a charm.

    0 讨论(0)
提交回复
热议问题