webhooksmicrosoft-dynamics

Howto register a webhook with AuthType=HttpHeader using "Dynamics 365 Web API"


To register a webhook on Dynamics 365 there is a tool called Plug-in Registration (https://learn.microsoft.com/en-us/powerapps/developer/data-platform/register-web-hook).

However, I would like to automate the webhook registration process and thus would like to use the Web API endpoint for webhook registration:

/api/data/v9.2/serviceendpoints

The problem I have is the setting of the value for the parameter authvalue in the JSON request body, because I want to use Http Header authtype:

{
    "name": "Test Webhook",
    "url": "https://myendpoint",
    "contract": 8,
    "authtype": 5,
    "authvalue": "<========= ??? what comes here ???"
}

Assuming I want to have this header be sent to my webhook endpoint:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

What would be the value of authvalue in the JSON above ? The value should be a string according to the data type of authvalue, but how that should be formatted?

If I use the value Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= as is for authvalue, when the webhook is fired, I got the following error in System Job:

Plugin Trace:

[Microsoft.Crm.ServiceBus: Microsoft.Crm.ServiceBus.WebhookPlugin] [ad9a4124-ab57-ec11-8f8f-6045bd8aed3b: Test for Step Creation]

Error Message:

System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The webhook call failed because of invalid http headers in authValue. Check if the authValue format, header names and values are valid for your Service Endpoint entity. (Fault Detail is equal to Exception details: ErrorCode: 0x80050203 Message: The webhook call failed because of invalid http headers in authValue. Check if the authValue format, header names and values are valid for your Service Endpoint entity. TimeStamp: 2021-12-08T10:15:26.8637496Z -- Exception details: ErrorCode: 0x80040216 Message: Received exception when adding custom http headers: for OrgId:xxxxxx-925f-4958-9aee-xxxxxxxxxxxx, serviceEndpointId: c099d16c-a057-ec11-8f8f-6045bd8aed3b, name: Test Webhook, exception:System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.LoadXml(String xml) at Microsoft.Crm.ServiceBus.WebhookClient.ExtractKe...).


Solution

  • For HtppHeader authentication type the key value pair for AuthValue must be set in the following xml format:

    <settings><setting name="Authorization" value="Basic dXNlcm5hbWU6cGFzc3dvcmQ=" /></settings>