How to set webhook in GAS?

For update a custom field soon after creating a task, I tried to set webhook and some code for update the task in Google Api Script.

First I set a webhook in GAS:

var token = <<<<<MY_TOKEN>>>>>;
var project_id = <<<<<MY_PROJECT_ID>>>>>;

var url = "https://app.asana.com/api/1.0/webhooks";

function myFunction() {
  var headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + token
  }

  var data = {
    "data": {
      "filters": [
        {
          "action":"added",
          "resource_type":"task"
        }
      ],
      "resource": project_id,
      "target": <<<<<TARGET_URL>>>>>
    } 
  }

  var options = {
    "method": "post",
    "payload": JSON.stringify(data),
    "headers": headers,
  }

  UrlFetchApp.fetch(url, options);
}


Then I made a function in Google Cloud Platform and set a code for handshake:

exports.handler = async (event) => {
    console.log('this is secret header ----->' + event.headers['X-Hook-Secret'])
    const response = {
        statusCode: 200,
        headers: {"X-Hook-Secret": event.headers['X-Hook-Secret']},
        body: JSON.stringify('Hello from GCP!'),
    };
    return response;
};

After deployed this function, I updated <<<<<TARGET_URL>>>>> to a triger URL of the function.

Then I pressed the RUN button of GAS, following error was returned.

Exception: Request failed for https://app.asana.com returned code 400. Truncated server response: {"errors":[{"message":"The remote server responded with an incorrect status code: 403","help":"For more information on API status codes and how to ... (use muteHttpExceptions option to examine full response)

How to solve this?

I’m not sure exactly what you mean by this. But the handshake has to occur from the same endpoint as the original webhook request came from. The target parameter doesn’t secify where Asana should go to do the handshake - again I can’t tell from your statement but I think maybe you’re thinking that’s what target is for - the target parameter tells Asana what endpoint to send the webhook events to, once the handshake is complete.

Does that help or am I misunderstanding your statement above?

1 Like