This document describes how to integrate the IDaaS SDK into your code to obtain an M2M client token.
Prerequisites
Before you begin, complete the environment preparation. For more information, see Environment preparation.
SDK initialization
The SDK initializes the IDaaS configuration by reading the configuration file that you specified during environment preparation.
IDaaSCredentialProviderFactory.init();All SDK features depend on the init() method, which you must call first. Otherwise, errors will occur when you obtain an IDaaSCredentialProvider, causing service interruptions.
The SDK attempts to retrieve an access token during initialization. If the configuration is missing or incorrect, this attempt fails, throwing an error and interrupting the service.
Obtain an access token
Obtain an IDaaS credential provider to retrieve an access token.
Use the parameterless constructor to obtain an IDaaS credential provider. This retrieves an access token for the scope specified in the configuration file.
IDaaSCredentialProvider credentialProvider = IDaaSCredentialProviderFactory.getIDaaSCredentialProvider();Use the parameterized constructor to obtain an IDaaS credential provider for a custom scope. The scope format is
audience ID|permission ID, corresponding to the audience ID and permission ID of the M2M server application you want to access.IDaaSCredentialProvider credentialProvider = IDaaSCredentialProviderFactory.getIDaaSCredentialProvider(scope);
The access token is of the bearer type. Retrieve it by calling the
getBearerToken()method on thecredentialProviderobject.String accessToken = credentialProvider.getBearerToken();
Code example
import com.cloud_idaas.core.factory.IDaaSCredentialProviderFactory;
import com.cloud_idaas.core.provider.IDaaSCredentialProvider;
public class sample {
public static void main(String[] args) {
// Initialize the IDaaS configuration from the configuration file.
IDaaSCredentialProviderFactory.init();
// Obtain a credential provider for the scope specified in the configuration file.
IDaaSCredentialProvider credentialProvider = IDaaSCredentialProviderFactory.getIDaaSCredentialProvider();
String accessToken = credentialProvider.getBearerToken();
// Obtain a credential provider for a custom scope.
// String scope = "api.example.com|read:file";
// IDaaSCredentialProvider anotherCredentialProvider = IDaaSCredentialProviderFactory.getIDaaSCredentialProvider(scope);
// String accessToken = anotherCredentialProvider.getBearerToken();
System.out.println(accessToken);
}
}