OAuth2 REST Connection Setup

Overview

Defines a connection to an external REST API using OAuth2 client credentials flow.

Connection File: connections/external-api.json

JSON
{
  "name": "externalApi",
  "type": "rest-oauth2",
  "url": "https://{{api.host}}",
  "authUrl": "https://{{api.authHost}}/oauth/token",
  "clientId": "{{api.clientId}}",
  "clientSecret": "{{api.clientSecret}}",
  "authRequestTemplate": "{\"grant_type\": \"client_credentials\", \"client_id\": \"::clientId\", \"client_secret\": \"::clientSecret\"}"
}

Properties:

api.host=api.example.com
api.authHost=login.example.com
api.clientId=my-client-id
api.clientSecret=my-client-secret

Using the Connection in Routes

XML
<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="fetchFromExternalApi">
        <from uri="timer://trigger?repeatCount=1"/>
        <to uri="pfx-rest:get?uri=/v1/products&amp;connection=externalApi"/>
        <to uri="pfx-api:loaddata?objectType=P&amp;mapper=productMapper"/>
    </route>
</routes>

How It Works

  1. On first request, IM calls authUrl with the authRequestTemplate to obtain an access token

  2. The token is cached and reused until it expires

  3. On expiry, IM automatically refreshes the token — no manual handling required

Common Pitfalls

  • authRequestTemplate uses ::clientId and ::clientSecret as placeholders (double colon, not {{}})

  • The url is the base URL for API calls; authUrl is only for token exchange

  • Different APIs may require different grant_type values — adjust authRequestTemplate accordingly