Search code examples
google-apigoogle-calendar-apigoogle-api-clientgoogle-api-python-client

Google Push notification giving ParseError


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 = '[email protected]'
    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 = '[email protected]'
    req = requests.post(url='https://www.googleapis.com/calendar/v3/calendars/[email protected]/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.


Solution

  • 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/[email protected]/events/watch', data = json.dumps(body), headers=headers)