-
Notifications
You must be signed in to change notification settings - Fork 34
How To Get an Authentication Token for System Users Java Code Sample
The following code sample, written in Java and leveraging the Spring Boot framework, programmatically manages the System User authentication process of obtaining an IBM Cloud IAM token and exchanging it for a Service ID token. You can utilize this code to help you get up and running quickly and start the process of automating the integration of your systems with the IBM Food Trust solution.
The code is a complete web service that can be deployed and run in your environment with a nominal configuration change. When the service starts, the API key stored in the configuration file is read and the token exchange process is executed. When the API is called, the expiration time on the token is checked and automatically renewed if it is expired. The inbound payload is then transmitted to IBM Food Trust and the result is returned.
The service utilizes Spring Boot and Gradle and was built for the following versions:
- Java 8
- Gradle
4.2.1
- Spring Boot
1.5.13.RELEASE
This code is provided to help you get up and running quickly with authentication and data submission to IBM Food Trust. This code will be maintained for any future authentication changes, but it is not recommended that this code be used "as-is". In an enterprise environment, additional requirements may be dictated by your organization (e.g. logging, unit tests, health endpoints, statistics endpoints, etc.).
You need to configure the service prior to running it, using the config.properties
file. A sample configuration file is shown below. The service analyzes the value of the iftEnvironment
variable; if the value is INTEGRATION
, the integration environment properties are used. In all other cases, the PRODUCTION
environment is assumed.
Specifically, you must configure the following variables:
- The API key and organization ID that were used in the initial token setup steps (detailed here)
- The IBM Food Trust environment ("INTEGRATION" or "PRODUCTION")
Sample config.properties file:
iftApiKey=8r...sv
iftEnvironment=INTEGRATION
iftCloudIAMURL=https://iam.cloud.ibm.com/identity/token
iftIntegrationOrgId=9dd5c...a1
iftIntegrationAuthURL=https://sandbox.food.ibm.com/ift/api/identity-proxy/exchange_token/v1/organization/
iftProductionOrgId=2af3b...d3
iftProductionAuthURL=https://food.ibm.com/ift/api/identity-proxy/exchange_token/v1/organization/
iftIntegrationURL=https://sandbox.food.ibm.com/ift/api/connector/v1/assets
iftProductionURL=https://food.ibm.com/ift/api/connector/v1/assets
Complete the following steps to configure your system users to automatically obtain and renew authentication tokens:
- Download the zip file and extract the contents to a working directory.
- cd into the directory
- Modify
src/main/resource/config.properties
(per the instructions above) - Execute
gradle wrapper
to build the project - Run the jar:
java -jar build/libs/IFT_authentication_sample-*.jar
- Post an XML payload to
localhost:8080
E.g.:
curl -X POST \
http://localhost:8080 \
-H 'Accept: application/json' \
-H 'Content-Type: application/xml' \
-d '<insert XML text here>'
WELCOME!
Modules
Membership
Languages
Browsers
ONBOARDING
Onboarding Steps
Data Requirements
Data Types
Supplier Data
Payload Data
Insights Data
HOW-TO
Join by Invitation
Log in as New User
Authenticate Human Users
Authenticate System Users
Java Sample
Typescript Sample
IIB Sample
Assign User Roles
Upload Data
Automate Data Upload
Convert Spreadsheets
Convert Data
Whitelist Custom URLs
APIs-Swagger
Connector API
Documents API
Converter API
Trace API
Insights API
APIs-Usage
APIs
Insights API
Insights API Usage
Trace API
Connector API Errors
API Error Codes
REFERENCE
GS1
GS1 Identifier Reuse
Authentication
Identifiers
Message Codes
Cryptographic
Signatures
Signature Header
Access Control
Firewall Settings
XML to JSON
EPCIS Aggregation Add
EPCIS Aggregation Delete
EPCIS Object Add
EPCIS Object Delete
EPCIS Object Observed
EPCIS Transformation
Purchase Order
Despatch Advice
Receiving Advice
Master Data Item
Master Data Facility
Standard Business
Document Header