Service CallOut for getting access token from external source

How to use Service CallOut for getting access token from external source. I have to write Service Callout for getting this token.

I need to pass below details in header:

Content-Type : application/x-www-form-urlencoded

Authorization : Basic (BASE64ENCODED)

I need to pass below details in Body:

grant_type : refresh_token

refresh_token : {ajaskdkjskjdksj}

Below is my code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="GetThirdPartyAccessToken">
    <Request variable="googleAuthRequest">
        <Payload contentType="application/x-www-form-urlencoded" variablePrefix="$" variableSuffix="%"><![CDATA[
grant_type=refresh_token&refresh_token=ajaskdkjskjdksj&client_id=CLIENT_ID&client_secret=VERY_SECRET&redirect_uri=myredirectURI
]]></Payload>
    </Request>
    <Response>calloutResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://wd2-impl-services1.workday.com/ccx/oauth2/mmc9/token</URL>
    </HTTPTargetConnection>
</ServiceCallout>
Solved Solved
0 4 1,016
1 ACCEPTED SOLUTION

Not applicable

your code should look similar to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request>
        <Set>
            <Headers>
                <Header name="Content-Type">application/x-www-form-urlencoded</Header>
                <Header name="Authorization">Basic xyz</Header>
            </Headers>
            <FormParams>
                <FormParam name="grant_type">refresh_token</FormParam>
                <FormParam name="refresh_token">{request.formparam.a}</FormParam>
            </FormParams>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://mocktarget.apigee.net/echo</URL>
    </HTTPTargetConnection>
</ServiceCallout>

View solution in original post

4 REPLIES 4

Not applicable

your code should look similar to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request>
        <Set>
            <Headers>
                <Header name="Content-Type">application/x-www-form-urlencoded</Header>
                <Header name="Authorization">Basic xyz</Header>
            </Headers>
            <FormParams>
                <FormParam name="grant_type">refresh_token</FormParam>
                <FormParam name="refresh_token">{request.formparam.a}</FormParam>
            </FormParams>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://mocktarget.apigee.net/echo</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Above looks correct with just few notes

  1. No need to set the content-type explicitly if you are using FormParams. Apigee sets it automatically. See docs here
  2. Try not to hardcode the Auth header in the policy. Use the Encrypted KVMs to fetch and then either use BasicAuth policy to encode them or else use the Message Template to encode them
  3. Use variables for request, something like
<Request clearPayload="true" variable="myRequest"> 

so that you have complete control on that request object

Hi,

I hardcoded the refresh_token in the service callout in place of {request.formparam.a} and then tested proxy via trace.

The overall proxy was in error with status 400.

However, I could see the access token generated in Service Callout Response.

But, when I try using POSTMAN by using POST method and URL as proxy end point I get invalid request error. However, if I use same proxy endpoint with all the details(Header, authorization, grant_type, refresh_token) in POSTMAN then I get access token.

Is this expected behaviour ?

Please note that API proxy only has ServiceCallout policy and nothing else.

Yes , it's expected.