Sample code for 30+ languages & platforms
Java

citi Developer OAuth2 Client Credentials Grant

See more OAuth2 Examples

Get access token for your application credentials. You can use this for citi APIs which do not require customer credential verification and consent (e.g. Onboarding).

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Implements the following CURL command:

        // curl --request POST \
        //   --url https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb \
        //   --header 'accept: application/json' \
        //   --user client-id:client-secret \
        //   --header 'content-type: application/x-www-form-urlencoded' \
        //   --data 'grant_type=client_credentials&scope=%2Fapi'

        http.setLogin("client-id");
        http.setPassword("client-secret");

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath("/gcb/api/clientCredentials/oauth2/token/us/gcb");
        req.setContentType("application/x-www-form-urlencoded");
        req.addParam("grant_type", "client_credentials");
        req.addParam("scope", "/api");
        req.addHeader("accept", "application/json");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpReq("https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb", req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        try {
            jResp.writeFile("qa_data/tokens/citi_client_credentials.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to save JSON access token file.");
            return;
        }

        //  Sample JSON response:
        //  (Sample code for parsing the JSON response is shown below)

        //  {
        //    "token_type": "bearer",
        //    "access_token": "AAIkMjdh ... 3fsWb7zJ0s",
        //    "expires_in": 1800,
        //    "consented_on": 1584817860,
        //    "scope": "/api"
        //  }

        //  Sample code for parsing the JSON response...
        //  Use the following online tool to generate parsing code from sample JSON:
        //  Generate Parsing Code from JSON

        String token_type = jResp.stringOf("token_type");
        String access_token = jResp.stringOf("access_token");
        int expires_in = jResp.intOf("expires_in");
        int consented_on = jResp.intOf("consented_on");
        String scope = jResp.stringOf("scope");
    }
}