1

I'm trying to automate an APIM policy deployment via Azure devops. The policy uses a client id & client secret to generate a bearer token for CRM. I've tried a number of ways of deploying the policy, ARM template, Azure Devops plugin from here https://github.com/stephaneey/azure-apim-extension. All of these fail with the same error.

Status Message: One or more fields contain incorrect values: (Code: ValidationError)
 - '=' is an unexpected token. The expected token is ';'. Line 11, position 76. (Code:ValidationError)

The policy I'm trying to deploy is this

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="bearerToken" timeout="20" ignore-error="true">
            <set-url>https://login.microsoftonline.com/{TENANT_ID}/oauth2/token</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@{
            return "client_id={CLIENT_ID_VALUE}&resource=https://{CRMINSTANCE}.dynamics.com/&client_secret={CLIENT_SECRET}grant_type=client_credentials";

           }</set-body>
        </send-request>
        <set-header name="Authorization" exists-action="override">
            <value>@("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])</value>
        </set-header>
        <set-backend-service base-url="https://{CRMINSTANCE}.dynamics.com/api/data/v9.1/" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
   <on-error>
        <base />
    </on-error>
</policies>

If I paste the policy xml directly into the APIM operation everything works as expected.

bristows
  • 736
  • 1
  • 7
  • 22
  • Have you tried APIM ARM template extractor tool? link: https://github.com/Azure/azure-api-management-devops-resource-kit/blob/master/src/APIM_ARMTemplate/README.md#extractor , This tool helps you to extract API as ARM template and then deploy it with DevOps. The policy deployment will be taken care in this. – Pankaj More Feb 07 '22 at 04:15
  • Whenever you get that type of error, try to fix it by replacing your & (ampersand) with the HTML encoded version (&) – K.J.M.O. Sep 27 '22 at 13:09

0 Answers0