【问题标题】:How can I send post request with json data on flask-dance?如何在烧瓶舞上发送带有 json 数据的发布请求?
【发布时间】:2019-04-02 10:28:16
【问题描述】:

我正在尝试在我的烧瓶舞蹈扩展上向谷歌日历的忙闲service 发送 POST 请求。但我收到“405 Method Not Allowed”错误。不知道怎么调试。

我必须在请求正文中传递 JSON 数据。我已提交请求documentation 。我是烧瓶和烧瓶舞的新手,任何帮助将不胜感激。

@app.route("/",methods=['POST'])
def free():
    if not google.authorized:
        return redirect(url_for("google.login"))
    event = json.dumps({"timeMin": "2019-03-31T00:00:00Z",
             "timeMax": "2019-04-01T00:00:00Z",
             "timeZone": " Asia/Calcutta",
             "groupExpansionMax": 3,
             "calendarExpansionMax": 1,
             "items": [
                 {
                     "id": "abcd@gmail.com"
                 }
             ]
            })
    resp = google.post(url="https://www.googleapis.com/calendar/v3/freeBusy",data=event)
    return resp.json()

响应应该是json数据

  "timeMax": "2019-04-01T00:00:00.000Z", 
  "kind": "calendar#freeBusy", 
  "calendars": {
    "abcd@gmail.com": {
      "busy": [
        {
          "start": "2019-03-31T03:00:00Z", 
          "end": "2019-03-31T09:00:00Z"
        }
      ]
    }
  }, 
  "timeMin": "2019-03-31T00:00:00.000Z"
}```

【问题讨论】:

  • 405 Method Not Allowed 通常是一个 cors 错误,但我没有看到您进行身份验证。你在哪里添加不记名令牌?
  • flask dance 通过使用 redirect(url_for("google.login")) 重定向到身份验证页面来处理它 PS这只是一个不起作用的端点。我有一个类似的 GET 端点,它似乎工作正常。 @DaImTo

标签: flask google-api google-calendar-api flask-dance


【解决方案1】:

添加了 GET 和 POST,现在代码似乎可以工作了


@app.route("/",methods=['GET','POST'])
def free():
    if not google.authorized:
        return redirect(url_for("google.login"))
    event = {"timeMin": "2019-03-31T00:00:00Z",
             "timeMax": "2019-04-01T00:00:00Z",
             "timeZone": " Asia/Calcutta",
             "groupExpansionMax": 3,
             "calendarExpansionMax": 1,
             "items": [
                 {
                     "id": "abcd@gmail.com"
                 }
             ]
            }

【讨论】:

    猜你喜欢
    • 2014-07-23
    • 2022-01-25
    • 1970-01-01
    • 2015-03-10
    • 2021-12-30
    • 2022-12-07
    • 2017-08-14
    • 2019-12-01
    • 2019-04-28
    相关资源
    最近更新 更多