extract the variable from my JSON payload and using Assign Message policy created a JSON payload so that i can customise my response style

Not applicable

Hello ,

I created a route "/token" where i use to generate access token from oAuth Generate Token Policy, but i need to customise my response which should contain only few stuffs. So in that same route i created extract variable policy to extract the necessary values and then create a Assign message payload policy and display it in my response.

Do i need to disable the response in Generate Token policy ?

Can anyone guide me on that, currently i have extracted but how to assign the message and also is this the correct way to customise the response?

Any suggestion or guidance is appreciated.

0 1 858
1 REPLY 1

Yes, you need to disable the response in the GenerateAccessToken policy. Fragment:

  <OAuthV2 name='OAuthV2-GAT'>
    <Operation>GenerateAccessToken</Operation>
    <GenerateResponse enabled='false'/>
    ...

If you include GenerateResponse and have enabled='true', then the response is sent directly to the caller. The payload looks like this:

      {
        "issued_at": "1420262924658",
        "scope": "READ",
        "application_name": "ce1e94a2-9c3e-42fa-a2c6-1ee01815476b",
        "refresh_token_issued_at": "1420262924658",
        "status": "approved",
        "refresh_token_status": "approved",
        "api_product_list": "[PremiumWeatherAPI]",
        "expires_in": "1799",
        "developer.email": "tesla@weathersample.com",
        "organization_id": "0",
        "token_type": "BearerToken",
        "refresh_token": "fYACGW7OCPtCNDEnRSnqFlEgogboFPMm",
        "client_id": "5jUAdGv9pBouF0wOH5keAVI35GBtx3dT",
        "access_token": "2l4IQtZXbn5WBJdL6EF7uenOWRsi",
        "organization_name": "docs",
        "refresh_token_expires_in": "0",
        "refresh_count": "0"
      }

If you omit GenerateResponse or have enabled='false', then these flow variables are set on success:

      oauthv2accesstoken.OAuthV2-GAT.access_token
      oauthv2accesstoken.OAuthV2-GAT.token_type
      oauthv2accesstoken.OAuthV2-GAT.expires_in
      oauthv2accesstoken.OAuthV2-GAT.refresh_token
      oauthv2accesstoken.OAuthV2-GAT.refresh_token_expires_in
      oauthv2accesstoken.OAuthV2-GAT.refresh_token_issued_at
      oauthv2accesstoken.OAuthV2-GAT.refresh_token_status

This assumes that the GenerateAccessToken policy is named "OAuthV2-GAT"; the names of the variables that get set depend on the name of the policy. You can then use an AssignMessage policy to set the message you want.

<AssignMessage name='AM-TokenInfo'>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType='application/json'>{
  "access_token" : "{oauthv2accesstoken.OAuthV2-GAT.access_token}",
  "refresh_token" : "{oauthv2accesstoken.OAuthV2-GAT.refresh_token}",
  "expires_in" : "{oauthv2accesstoken.OAuthV2-GAT.expires_in}"
}
</Payload>
    <StatusCode>200</StatusCode>
    <ReasonPhrase>OK</ReasonPhrase>
  </Set>
</AssignMessage>

There's no need for ExtractVariables, with this approach.