Skip to main content
Version: 5.6

Connecting with a Bearer Token

This example shows how a Bearer token is first retrieved via a REST call and then used in a second connection for further REST calls.

Retrieving the Token

First, create the input values required for the token request. The parameters required depend on the target system. Typical examples include client_id, client_secret, scope, username, password, or grant_type.

Many OAuth 2.0 token endpoints expect a POST request for this. The application/x-www-form-urlencoded or application/json format is often used for this. In our example, we use application/json.

To do this, proceed as follows: Write JSON for Bearer Token

  1. Click the Edit button to insert the default JSON structure for the token request. Then click Edit again to apply the content. In our example, the token request structure looks like this:
{
"grant_type": "client_credentials",
"client_id": "my-client",
"client_secret": "my-secret",
"scope": "api.read"
}
  1. Now enter the correct values for client_id and client_secret in the Value column.

Then configure a connection using a REST client transfer object:

Example configuration of the REST client transfer object for the token query

  1. Select the appropriate REST client connection.
  2. In the REST connection, set the authentication to None if the token is requested via the request itself and no HTTP authentication is required.
  3. Enter the required URL endpoint.
  4. Select POST as the HTTP method.
  5. In the Request tab, select the format expected by the token endpoint. In our example, Body type = raw and the format is application/json.
  6. Pass the required values by connecting the JSON parameter of the JSON Write transfer object to the request body.

Now we need the JSON Read Transfer Object:

JSON Read Transfer Object for Bearer Token

Next, create a variable (untyped) in which the response from the token endpoint is initially stored in its entirety. To do this, connect the response’s body parameter to this variable.

Then, use a JSON Read Transfer Object to read at least the access_token value from this response. If the target system also returns values such as token_type, expires_in, or refresh_token, you can also read these and use them further.

If the token needs to be renewed regularly, integrate a time trigger and set the interval so that the request is executed again before the token expires.

In the end, our example connection looks like this:

Connection for Bearer Token

Passing the Token

In a second connection, pass the previously retrieved token to another REST call.

  1. Drag the variable containing the stored token response onto the workspace.
  2. Add a JSON Read transfer object and read at least the access_token value from it.
  3. If necessary, also read token_type. If the target system does not return a type, generally use Bearer.
  4. Create a Formula and Calculator transfer object and select the CONCATENATE function via More. Combine the header value from token_type, a space, and access_token. If the type is not provided dynamically, you can instead concatenate the fixed prefix Bearer with the access_token.

Formula and Calculator Transfer Object for Bearer Token

Connect the output values of the JSON Read transfer object to the inputs of the Formula and Calculator transfer object.

Next, design another REST client transfer object:

  1. Select a method such as GET to retrieve your actual data.
  2. Select the application/json format for the request.
  3. Create a request parameter of type HTTP Header.
  4. Enter Authorization as the header name.
  5. Link the output value of the formula and calculator transfer object to this request parameter.
  6. Then link the body parameter of the response to the desired target object.
  7. If the call is to be made cyclically, integrate a time trigger.

Connection for passing the Bearer token

Sample Project for Import

The complete sample project is available as rest_bearer_token_and_production_data_example.rpe.

The project file contains the Bearer token configuration shown, as well as the two REST trigger connections from the example Providing Simulated Production Data via REST Triggers.

Import this project file into the OPC Router to fully replicate the example and test it directly.