Using Service CallOut to Access Google OAuth For Retrieving Access Token

Not applicable

Hi,

I am trying to invoke the Google OAuth 2.0 to retrieve the access token by providing the authorisation code, client id ,grant type,secret key,redirect uri from a service call out policy.

I managed to get the Authorisation code from the Google OAuth , but the POST request to get the access token fails with

{
    "fault": {
        "detail": {
            "errorcode": "steps.servicecallout.ExecutionFailed"
        },
        "faultstring": "Execution of ServiceCallout GetThirdPartyAccessToken failed. Reason: ResponseCode 400 is treated as error"
    }
}

However when I tried the same POST request with the authorisation code and other parameters via CURL, I am able to get the access token back successfully.

curl -X POST https://www.googleapis.com/oauth2/v3/token \ 
   -d "grant_type=authorization_code&code=4/bVMzsBtBCIKZ7c1WSgxZUf2kIjbSIRJ3vxEdz2Gufqk&client_id=Client_ID_HERE&client_secret=VERY_SECRET&redirect_uri=myredirectURI"

Find below my ServiceCall Out.

I am not sure , if the Google OAuthAPI accepts a json payload.(Pls correct me )

<ServiceCallout name="GetThirdPartyAccessToken"> 
  <Request variable="googleAuthRequest">
    <Set>
      <Headers>
        <Header name="Accept">application/json</Header>
      </Headers>
      <Payload contentType="application/json" variablePrefix="$" variableSuffix="%"> {
      "code": "$request.queryparam.code%"
      "client_id":"myclient id"
      "client_secret":"mysecret"
      redirect_uri:"my redirect uri"
      "grant_type":"authorization_code"
      }
      </Payload>
      <Verb>POST</Verb>
    </Set>
  </Request>
  <Response>calloutResponse</Response>
  <Timeout>30000</Timeout>
  <HTTPTargetConnection>
    <Properties/>
  <URL>https://www.googleapis.com/oauth2/v3/token</URL> </HTTPTargetConnection>
</ServiceCallout>

Could you please help me correcting my service call out to Google OAuthAPI to get the AccessToken via a POST request?

Solved Solved
1 5 1,721
1 ACCEPTED SOLUTION

You already have it 🙂 Just use "grant_type=authorization_code&code=4/bVMzsBtBCIKZ7c1WSgxZUf2kIjbSIRJ3vxEdz2Gufqk&client_id=<<Client ID from DEv console>&client_secret=<From Dev console of Google>Q&redirect_uri=myredirectURI" instead of the json inside <payload> and change the content-Type.

<ServiceCallout name="GetThirdPartyAccessToken"> 
  <Request variable="googleAuthRequest">
    <Set>
      <Payload contentType="application/x-www-form-urlencoded"
               variablePrefix="$"
               variableSuffix="%"><![CDATA[
grant_type=authorization_code&code=CODE_GOES_HERE&client_id=CLIENT_ID&client_secret=VERY_SECRET&redirect_uri=myredirectURI
]]></Payload>
      <Verb>POST</Verb>
    </Set>
  </Request>
  <Response>calloutResponse</Response>
  <Timeout>30000</Timeout>
  <HTTPTargetConnection>
    <Properties/>
    <URL>https://www.googleapis.com/oauth2/v3/token</URL>
  </HTTPTargetConnection>
</ServiceCallout>


View solution in original post

5 REPLIES 5

Hi @Nikkie I am not expert on google APIs , but there is a discrepancy which I see above :

The curl command uses application/x-www-form-urlencoded for sending the data whereas the service callout uses an application/json payload. I do not know if google supports both but I will assume it does not.

So change the actual payload in the service callout to the same x-www-form-urlencoded format and give it a shot.

Hi @sarthak

Thanks for reply.

I am new to Apigee , could you please help me create the payload in the x-www-form-urlencoded format so that it can be used in the ServiceCallOut.

You already have it 🙂 Just use "grant_type=authorization_code&code=4/bVMzsBtBCIKZ7c1WSgxZUf2kIjbSIRJ3vxEdz2Gufqk&client_id=<<Client ID from DEv console>&client_secret=<From Dev console of Google>Q&redirect_uri=myredirectURI" instead of the json inside <payload> and change the content-Type.

<ServiceCallout name="GetThirdPartyAccessToken"> 
  <Request variable="googleAuthRequest">
    <Set>
      <Payload contentType="application/x-www-form-urlencoded"
               variablePrefix="$"
               variableSuffix="%"><![CDATA[
grant_type=authorization_code&code=CODE_GOES_HERE&client_id=CLIENT_ID&client_secret=VERY_SECRET&redirect_uri=myredirectURI
]]></Payload>
      <Verb>POST</Verb>
    </Set>
  </Request>
  <Response>calloutResponse</Response>
  <Timeout>30000</Timeout>
  <HTTPTargetConnection>
    <Properties/>
    <URL>https://www.googleapis.com/oauth2/v3/token</URL>
  </HTTPTargetConnection>
</ServiceCallout>


I tried the below , but the UI says Invalid XML as expected.

<Payload variablePrefix="$" variableSuffix="%" contentType="application/x-www-form-urlencoded" >

grant_type=authorization_code&code=$request.queryparam.code%&client_id=myclient id &client_secret=mysecret&redirect_uri=https://nikkiethomas-test.apigee.net/web/callback

</Payload>

I am unsure on how to give the details in the ServiceCallOut Payload.

This should work ::

<![CDATA[ grant_type=authorization_code&code=$request.queryparam.code%&client_id=myclient id &client_secret=mysecret&redirect_uri=https://nikkiethomas-test.apigee.net/web/callback ]]>