Authentication using Apex Call
This topic provides the details and sample code to grab your Authentication bearer token from Conga Platform in order to adopt document generation through Apex calls via Composer for Advantage Platform.
Create Salesforce Apex Class
Login to Salesforce as an Administrator.
Open the Developer Console by clicking your profile icon in the upper right and select
Developer Consolefrom the dropdown menu.In the Developer Console, click
File, selectNew > Apex Classto create a new apex class.In the dialogue that appears, enter the class name: CongaDocumentManager and click
OK.
Create Solution in CongaDocumentManager Apex Class
First, you must define each variable you are using for the solution, using the following three essential variables.
- private static final String clientId = 'Your Client ID here'
- private static final String clientSecret = 'Your Client Secret here'
- private static final String authUrl = 'https://login-rls.congacloud.com/api/v1/auth/connect/token'
Next, create a null variable that will contain the value of the Bearer token. Name this what you would like to – below is an example: private static String authToken.
Once you have created this variable, you will create the first method to initiate the Merge process:
public static void initiateMerge() {
//This is starting the merge process
getAuth();
}
Explanation: the initiateMerge() method serves as the entry point for starting the authorization process. By wrapping the getAuth() call within it, you enable external invocation of initiateMerge(), making it easy to initiate the entire workflow.
Upon calling initiateMerge(), this invokes the getAuth() method. This design allows for flexibility in the implementation, as you can expand the authorization capabilities in the future. Once the merge process is complete, you can seamlessly integrate additional Conga products, such as Composer or Sign. This approach provides a structured way to gradually build out a comprehensive solution.
Creating the Method
Now that the getAuth() call is invoked, you can start the creation of this Method. Here are your goals when building out this method:
- Method Declaration
- Creating an HTTP request
- Setting an Endpoint, Request Method, Header and Body for the API call
- Create an Http container to hold the response
- Creating an Http Instance
- Send out an Http request
- Checking the Response Status
- Deserializing the Response Body
- Retireiving the Access token
- Logging the Token
- Create Failing Conditions
Method Declaration - public static void getAuth()
The method getAuth() is declared as private,
meaning it can only be accessed within the same class. It's static, indicating
that it belongs to the class itself and belongs to the class, rather than an
instance of the class.
Creating an HTTP Request - HttpRequest req = new HttpRequest();
A new instance of HTTP Request in created. This object is used to configure and send our HTTP request to the Conga API.
Setting Endpoint URL - req.setEndpoint(authURL);
The endpoint for the HTTP request is set to the authURL, which is a predefined string containing the authorization URL for the Conga API.
Specifying the HTTP Method - req.setMethod('POST');
The HTTP method is set to POST. This method is commonly used for sending data to a server, such as credentials in this case.
Setting Request Headers - req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
The Content-Type header is specified. This indicates the type of data being sent in the body of the request, which, in this case, is URL-encoded form data.
Setting the Request Body - req.setBody('grant_type=client_credentials&client_id=' + clientId + '&client_secret=' + clientSecret);
- grant_type: Specifies that we are using client credentials.
- client_id: Your unique client identifier.
- client_secret: The secret key associated with your client ID.
- This body will be sent to the authentication endpoint to obtain an access token.
Creating an HTTP Instance - Http http = new Http();
An instance of the Http class is created. This object will be used to send the HttpRequest.
Sending the HTTP Request - HttpResponse res = http.send(req);
The send() method is called on the http object, passing the req object. This sends the configured HTTP request and captures the response in the variable res.
Responses for the API Requests
Now we can successfully send out an API request, you will make responses to show us the status of the API request
Checking
the Response Status - if (res.getStatusCode() == 200) {
The method checks if the HTTP response status code is 200, which indicates a successful request. If it is not successful, this statement will not execute.
Deserializing
the Response Body - Map<String, Object> jsonResponse =
(Map<String, Object>)JSON.deserializeUntyped(res.getBody());
Retrieving
the Access Token - authToken = (String)
jsonResponse.get('access_token');
Logging
the Token - System.debug('Auth token received: ' +
authToken);
Create
failing conditions - else { }
Example Code
Using the above sections, here is an example of an Apex Call.
public class CongaDriveCall {
private static final String clientId = 'Put your client id here';
private static final String clientSecret = 'Put your client secret here';
private static final String authUrl = 'https://login-rls.congacloud.com/api/v1/auth/connect/token';
private static String authToken;
public static void initiateAuth() {
//Inititate the Authorization process
getAuth();
}
private static void getAuth() {
//Create the HTTPRequest
HttpRequest req = new HttpRequest();
//Set the HTTP Endpoint
req.setEndpoint(authURL);
//Set the HTTP Method
req.setMethod('POST');'
//Set the Header
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
//Set the Body
req.setBody('grant_type=client_credentials&client_id=' + clientId + '&client_secret=' + clientSecret);
//Initialize new HTTP response
Http http = new Http();
//Call the Endpoint with the set parameters we defined
HttpResponse res = http.send(req);
//Check to see if call was a success/ If not it throws an error to our system debug
if (res.getStatusCode() == 200) {
Map<String, Object> jsonResponse = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
authToken = (String) jsonResponse.get('access_token');
System.debug('Auth token received: ' + authToken);
} else {
System.debug('Failed to obtain token: ' + res.getBody());
}
}
}
