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

Last updated

Was this helpful?