> For the complete documentation index, see [llms.txt](https://sahamati.gitbook.io/aa-common-service/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sahamati.gitbook.io/aa-common-service/token-service/code-snippets.md).

# Code Snippets

### Generating the access token

```
public Object generateToken() throws Exception {
	try {
		String client_id = clientId;
		String client_secret = clientSecret;
		String grant_type = "client_credentials";
		URL url = null;
		InputStream stream = null;
		HttpURLConnection urlConnection = null;
		url = new URL(generateTokenUrl);
		urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.setRequestMethod("POST");
		urlConnection.setDoOutput(true);
		String data = URLEncoder.encode("client_id", "UTF-8") + "=" + URLEncoder.encode(client_id, "UTF-8");
		data += "&" + URLEncoder.encode("client_secret", "UTF-8") + "=" + URLEncoder.encode(client_secret, "UTF-8");
		data += "&" + URLEncoder.encode("grant_type", "UTF-8") + "=" + URLEncoder.encode(grant_type, "UTF-8");
		urlConnection.connect();
		OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
		wr.write(data);
		wr.flush();
		stream = urlConnection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 8);
		String result = reader.readLine();
		if (!NullOrEmptyUtil.isNullOrEmpty(result)) {
			return result;
		}
		return null;
	} catch (Exception e) {
		throw e;
	}
}
```

### **Decoding the access token**

```
String jwtToken = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJaWWVIMnR2UmtLS3o2dFhYRVo0R2RaQjh5a0FXOHNLWnoyUXlULU1vUEVFIn0.eyJleHAiOjE2MDA0MjYyNTksImlhdCI6MTYwMDMzOTg1OSwianRpIjoiYmI3MDQ0MmItYjcyYy00MTQ5LWE1OTYtMDc2ZDkyMTg5OTE0IiwiaXNzIjoiaHR0cHM6Ly90b2tlbnMuc2FoYW1hdGkub3JnLmluL2F1dGgvcmVhbG1zL3NhaGFtYXRoaSIsImF1ZCI6ImFjY291bnQiLCJzdWIiOiI4Njl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ruoBM07gIcIoItTgB7kc81pfuaOpSE9gc7MVRRoxS8AwE0ZiqA2h3Pui4UP-0PsgOI_ZgqGGrbwe7Hr1__vw";

System.out.println("------------ Decode JWT ------------");

String[] split_string = jwtToken.split("\\.");
String base64EncodedHeader = split_string[0];
String base64EncodedBody = split_string[1];
String base64EncodedSignature = split_string[2];

System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
Base64 base64Url = new Base64(true);
String header = new String(base64Url.decode(base64EncodedHeader));
System.out.println("JWT Header : " + header);

System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
String body = new String(base64Url.decode(base64EncodedBody));
System.out.println("JWT Body : " + body);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sahamati.gitbook.io/aa-common-service/token-service/code-snippets.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
