0

I am trying to use google push notification for calendar to be notified if a user's event's start or/and end dateTime changes.

I have followed all the steps from push doc regarding registering and verifying my domain.

My code is as follows:

@blueprint.route("/notifications", methods={'GET','POST'})
def timeBlocker():
    email = '....@gmail.com'
    user = User.query.filter_by(email=email).first()  
    thecredentials = { 
    'client_id': os.getenv('client_id'),
    'client_secret':os.getenv('client_secret'),
    'refresh_token':user.refresh,
    'grant_type': 'refresh_token',
    }
    req = requests.post(url='https://oauth2.googleapis.com/token', data = thecredentials) 
    req_ = req.json()
    accessToken = req_["access_token"]
    print('access token is ' + accessToken)
    body = {
        'id': '01234567-89ab-cdef-0123456789ab', 
        'type': 'web_hook',
        'address': 'https://dayliii.com/notifications'
    }
    headers = {'Authorization': 'Bearer ' + accessToken,
               'Content-Type': 'application/json'
               }
    calendar = '....@group.calendar.google.com'
    req = requests.post(url='https://www.googleapis.com/calendar/v3/calendars/....@group.calendar.google.com/events/watch', data = body, headers=headers) 
    return str(req.json())

Error:

{'error': {'errors': [{'domain': 'global', 'reason': 'parseError', 'message': 'Parse Error'}], 'code': 400, 'message': 'Parse Error'}}

I tried converting from double quotes to single quote as this similar post suggested yet it didn't work.

Lastly, I was curious to know if I should register & verify domain ownership of 'http://localhost:5000/' when working with push notifications in dev mode? As that's the error I am expecting to get that not sure about the way around it.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Daniyal dehleh
  • 2,314
  • 2
  • 7
  • 22

1 Answers1

1

The data parameter in request() function accepts json format.

You can try converting your body variable into a json string using json.dumps().

Your request code should look like this:

req = requests.post(url='https://www.googleapis.com/calendar/v3/calendars/....@group.calendar.google.com/events/watch', data = json.dumps(body), headers=headers) 
Jason E.
  • 1,201
  • 1
  • 3
  • 10
  • Thanks so much. Do you know how I shall deal with the localhost issue by chance too? As I get `WebHook callback must be HTTPS: http://localhost:5000/notifications'` in dev mode & not sure how else I can debug w/o it. – Daniyal dehleh Apr 01 '21 at 03:26
  • You should add your domain in the developers console. You can refer to the answer on this post for that one. https://stackoverflow.com/questions/23928758/google-push-notifications-unauthorized-webhook-callback-channel – Jason E. Apr 01 '21 at 15:44
  • By the way, is it possible for you to check my last push notification error https://stackoverflow.com/questions/67308478/google-push-notification-reason-notfound . Really means the world. – Daniyal dehleh Apr 29 '21 at 20:16