I am trying to add a oauth2 callout to my azure apim policy. I do a <send-request>
<send-request mode="new" response-variable-name="tokenResponse" timeout="20" ignore-error="false">
<set-url>{{oauth2-token-url}}</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 "grant_type=client_credentials&client_id={{oauth2-client-id}}&client_secret={{oauth2-client-secret}}";}</set-body>
</send-request>
This callout works. Next i want to store the accessToken which is located in the tokenResponse but I can't get the variable to work. I am trying to cast it like:
<set-variable name="accessToken" value="@(((IResponse)context.Variables["tokenResponse"]).Body.As<JObject>()).SelectToken("access_token").ToString()" />
but this doesn't seem to work. Can anyone elaborate on this. How do I cast from Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.PipelineResponse to a JSON object and retrieve the access_token property.
Thanks in advance!