【问题标题】:Google Push notification giving ParseError谷歌推送通知给 ParseError
【发布时间】:2021-06-27 16:03:39
【问题描述】:

我正在尝试使用谷歌推送通知来通知用户活动的开始或/和结束日期时间是否发生变化。

我已按照push doc 中关于注册和验证我的域的所有步骤进行操作。

我的代码如下:

@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': {'errors': [{'domain': 'global', 'reason': 'parseError', 'message': 'Parse Error'}], 'code': 400, 'message ': '解析错误'}}

我尝试将双引号转换为单引号,因为 similar post suggested 但它不起作用。

最后,我很想知道在开发模式下使用推送通知时是否应该注册并验证“http://localhost:5000/”的域所有权?因为这是我期望得到的错误,所以不确定如何解决它。

【问题讨论】:

    标签: google-api google-calendar-api google-api-client google-api-python-client


    【解决方案1】:

    request()函数中的data参数接受json格式。

    您可以尝试使用 json.dumps() 将您的 body 变量转换为 json 字符串。

    您的请求代码应如下所示:

    req = requests.post(url='https://www.googleapis.com/calendar/v3/calendars/....@group.calendar.google.com/events/watch', data = json.dumps(body), headers=headers) 
    

    【讨论】:

    • 非常感谢。你知道我也将如何处理 localhost 问题吗?当我在开发模式下得到WebHook callback must be HTTPS: http://localhost:5000/notifications' 时,不确定我还能如何调试它。
    • 您应该在开发者控制台中添加您的域。你可以参考这篇文章的答案。 stackoverflow.com/questions/23928758/…
    • 顺便问一下,您可以检查我上次的推送通知错误stackoverflow.com/questions/67308478/…。真正意味着世界。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2015-09-28
    • 2013-08-20
    相关资源
    最近更新 更多