【问题标题】:How to setup a webhook on Asana API如何在 Asana API 上设置 webhook
【发布时间】:2017-06-10 07:30:33
【问题描述】:

我正在尝试使用以下内容在 Asana 上设置 webhook:

token = <user_token>
uri = URI.parse("https://app.asana.com/api/1.0/webhooks")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{token}"
request.set_form_data(
  "resource" => "219668070168571",
  "target" => "https://myserveraddress/api/webhooks/asana/1234",
)

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

puts response.body
puts response.code

这是我处理响应的控制器:

response.set_header("X-Hook-Secret", request.headers["X-Hook-Secret"])
head 200

当我执行 curl 请求时,例如:

curl -i --header "X-Hook-Secret: 12356789" https://myserveraddress/api/webhooks/asana/1234

我得到以下答案:

HTTP/1.1 200 OK
X-Hook-Secret: 12356789
Content-Type: text/html
Cache-Control: no-cache
X-Request-Id: fd8ec280-9ef1-426c-9cb5-58309f835ccf
X-Runtime: 0.045875
Vary: Origin
Transfer-Encoding: chunked

但是当我尝试设置 webhook 时,我从 Asana 收到了以下响应:

{"errors":[{"message":"Could not complete activation handshake with target URL. Please ensure that the receiving server is accepting connections and supports SSL","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

我在这里错过了什么?

【问题讨论】:

    标签: ruby asana asana-api


    【解决方案1】:

    嘿@WagnerMatosUK,

    您的回复在我看来不错,所以这可能不是问题所在。不过,我怀疑发生了什么……

    webhook 握手的顺序如下:

    client                    server
    |-- POST /api/1.0/webhooks --->|
                                   |
    |<--- POST {yourcallback} ---- |
    |                              |
    |--- 200 OK [with secret] ---> |
                                   |
    <--------- 200 OK -------------|
    

    也就是说,回调发生在原始请求返回之前内部

    这对于确保 webhook 已建立非常有用,因为第一个请求的返回将是成功还是失败,具体取决于握手发生的情况。但是,这也意味着您的服务器需要能够在第一个请求返回之前响应传入的握手请求。

    这对在第一个请求返回时阻塞的单线程服务器有影响——也就是说,它们被阻塞了!他们无法响应第二个请求直到第一个请求返回。

    解决方案是在线程中启动第一个请求,从而释放第二个线程,或者以某种方式并行运行多个服务器(如Unicorn 所做的),以便进程 2 中的服务器可以处理进程 1 被阻塞时握手。

    希望这有意义并解决问题!干杯!

    【讨论】:

    • 嗨,马特。感谢您的回复,很抱歉我花了这么长时间才对此发表评论。您的回复确实有道理,但是我的服务器已经线程化(我使用 Puma),这意味着原始请求没有阻塞第二个。
    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2016-01-23
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多