I'm trying to create asana webhook through apex.
I keep failing the authentication phase - handshake:
{"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"}]}
I'm missing some thing and not sure what.
I tried to create a class with @RestResource notation for handshake post callback:
@RestResource(urlMapping = '/AsanaWeb/*')
global class asanaCallbackCtrl {
public asanaCallbackCtrl(system.RestRequest req) {
}
@HttpPost
global static void doPost() {
asanaCallbackCtrl ctrl = new asanaCallbackCtrl(RestContext.request); //asanaCallbackCtrl.newInstance(RestContext.request);
RestResponse resp = ctrl.executePost();
RestContext.response.statusCode = resp.statusCode;
RestContext.response.responseBody = resp.responseBody;
// the record not inserted to database..
Log__c log = new Log__c();
log.Body__c = JSON.serialize(resp.responseBody);
log.Response_Hedears__c = JSON.serialize(resp.headers);
log.Staus_Code__c = string.valueOf(resp.statusCode);
insert log;
}
public RestResponse executePost() {
RestResponse resp = new RestResponse();
RestResponse res = RestContext.response;
try {
Blob targetBlob = Blob.valueOf(res.headers.get('X-Hook-Secret'));
// Generate SHA1 digest
Blob hashSHA1 = Crypto.generateDigest('hmacSHA1', targetBlob);
// For Rackspace Compatiblity encode the binary into BASE 64
// this will result in 28 chars long string
String hashBase64SHA1 = EncodingUtil.base64encode (hashSHA1);
resp.addHeader('X-Hook-Secret', res.headers.get('X-Hook-Secret'));
// verify the incoming request as legit; throw exception otherwise
resp.statusCode = 200;
}
catch (Exception e) {
resp.statusCode = 500;// set per doc (403 or 500)
}
// persist some log so you have a record of the incoming request
return resp;
}
/**
* Create a new task using Asana API
*/
public static HttpResponse createWebhook() {
object resource = '486792005021811';
string target = 'https://bringgsb-bringg.cs17.force.com/AsanaWeb/services/apexrest/AsanaWeb/486792005021811';
NewWebhook newHook = new NewWebhook(resource, target);
//NewTask newTask = new NewTask(new string[]{'139836167870950'}, '6125975198930', 'Lee Test Api', 'Test Notes');
String JSONString = JSON.serialize(newHook);
JSONString = '{"data":' + JSONString + '}';
HttpResponse res;
string base64Key = createAuthorizationHeader('0/859eb48d9719099d88cb2cfab4dbcb2e');
/**
* API call cannot be made while test class in running.
* so setting the expected response on which code would run
*/
if (Test.isRunningTest()) {
} else {
res = sendHttp(asanaEndPoint + '/webhooks', 'POST', JSONString , 'Basic ' + base64Key);
}
return res;
}
}
I added this class to my site (Enable apex classes), but not sure if I put the correct url for my target: (i'm running in sandbox)
https://**[siteURL?]**/services/apexrest/**[domainName?]**/AsanaWeb/486792005021811