2

I'm trying to set up a webhook in Asana to send me event updates for a particular project. I am pretty novice so keep that in mind when reading and answering. My first post on here so go easy on me. Here's my code I am running.

asanawebhook.php page:

$headers = getallheaders();
$secret_token = $headers['X-Hook-Secret'];
header('X-Hook-Secret: ' . $secret_token);
header("HTTP/1.1 200 OK");

my curl request to create the webhook:

$apikey = "mykey"; // Your API key
$taskid = "resourceid";

exec( 'curl \
-H "Authorization: Bearer '.$apikey.'" \
-H "Content-Type: application/x-www-form-urlencoded" \
-X POST https://app.asana.com/api/1.0/webhooks \
-d "resource='.$taskid.'" \
-d "target=https://mywebsite.com/folders/asanawebhook.php"', $return);

print_r($return);

This has been getting me this error printed on my page.

Array ( [0] => {"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"}]} )

If I can get that to return a success message of some kind, I assume I can parse the data and then do a curl request back to Asana to get the full payload. Thanks in advance.

2 Answers2

1

Thanks for reaching out! I'm a Developer Advocate at Asana, and I think I know what could be causing troubles for you. (And no worries, we're quite friendly over here at Asana!)

When you create a webhook, what you're asking Asana to do is to call out to you when something you're interested in changes state. That is, when something happens to change the project you have the webhook registered for, our servers will call your server to let you know that the change has happened.

This means that our servers need to get in contact with a publicly-available server that you maintain. To check that this is the case, when you create a webhook, there's a "handshake" process:

  1. You make a call to Asana's API to create a webhook. This blocks temporarily, while:
  2. Asana's servers call you back with a POST request to the location you specify in target with a header set to a value, which:
  3. You respond to with a 200 OK response with the same header value, and finally
  4. The original call to create the webhook returns with 200 OK.

If you don't have this handshake set up, then the return call from the webhook creation will fail with the error that you're seeing. You can see more technical detail about this process on our API reference page for webhooks. It may seem like a lot of work or back-and-forth to get set up, but this process is designed so that you know when you create a webhook whether or not you'll get future callbacks, rather than sad, silent failures - we actually test that you can when you create the webhook.

As an alternative, if you don't want to create and maintain a publicly-available server, you might want to check out our events API; these are much like webhooks, but rather than you expecting to get called by Asana's servers when things change, you can poll for changes since a particular point in time (as represented by a sync token that we give you). For instance, you can GET a task at a particular point in time, and also GET a sync token for that task from the /events endpoint. Periodically after that, you can create GET requests on the same resource at the /events endpoint and pass that sync token, and we'll return what (if anything) has changed since you got that token (and pass back a new "up to date after these changes have happened" token for you to use in future calls).

I hope this helps get you moving again!

Matt
  • 10,434
  • 1
  • 36
  • 45
  • Thank you Matt for looking into this. I understand that I need do the handshake. That's what I am hoping to understand better. From my googling around this weekend I was able to put together the above code. When I send my request to create a webhook, does my connection with that page stay open? In other words do I need to put the bits about the secret token after the request to create a webhook? If so, is the intention that I would remove the code to setup a webhook once it's created? – Danny Ramirez Oct 10 '16 at 12:12
  • It was pointed out to me that I have a erroneous url in there. I didn't realize that's what you were referring to. I don't actually have someurl in my code I just put that in there to shorten it... It's a legit, public url in that field in my code. I contacted Asana via email also and they said they'd forward you my info. Just wanted to clarify in case others read this. – Danny Ramirez Oct 10 '16 at 18:14
  • @DannyRamirez After creating handshake, did you get response consistent? – Hardik Patel May 31 '18 at 03:14
1

After carefully researching and polling Asana for help, I have determined that it was an SSL related problem for me. The code works great and allows the handshake to take place and sets up the webhook. What I didn't realize was that though I had an SSL cert I did not have it set up. Make sure SSL is configured correctly for the domain that the webhook target is going to.