How to get OAuth 2.0 Bearer Access Token |
Hello everyone, Welcome to techxon. In this Java tutorial we discuss how to get OAuth 2.0 Bearer Access Token using Java HTTP POST request. Simply you can use this java class for get OAuth 2.0 bearer access token. This POST request Content-Type is "application/x-www-form-urlencoded". First you need some important parameters for this make this request.
- grant_type
- client_id
- client_secret
These parameters are mandatory for this application/x-www-form-urlencoded POST request. Let's discuss step by step.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataParams.length()));
Complete Java Class
- Encode parameters
//Encode parameters
String grant_type = URLEncoder.encode("your_grant_type", "UTF-8");
String client_id = URLEncoder.encode("your_id", "UTF-8");
String client_secret = URLEncoder.encode("your_secret", "UTF-8");
- Create request body data URL
//Request body data
String postDataParams = "grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;
- Open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataParams.length()));
- Write data
//Write data
OutputStream outStream = connection.getOutputStream();
outStream.write(postDataParams.getBytes());
- Read response
//Read response
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
- Close BufferedReader and OutputStream
//close BufferedReader and OutputStream
br.close();
outStream.close();
- Print response
//print response
System.out.println("Response : " + strBuilder.toString());
- Response converted to JSON object
//response converted to JSON object
JSONObject jOb = new JSONObject(strBuilder.toString());
//print JSON object
System.out.println("JSON Object : " + jOb);
- Print Bearer Access Token
//get access token
String access_token = jOb.getString("access_token");
//print access token
System.out.println("Bearer Token : " + access_token);
Complete Java Class
package yourpackagename;
//imports
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author Gayan Ruchiranga
*/
public class GetBearerAccessToken {
public static void main(String[] args) {
try {
//Encode parameters
String grant_type = URLEncoder.encode("your_grant_type", "UTF-8");
String client_id = URLEncoder.encode("your_id", "UTF-8");
String client_secret = URLEncoder.encode("your_secret", "UTF-8");
//Request body data
String postDataParams = "grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;
URL url = new URL("your_url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataParams.length()));
//Write data
OutputStream outStream = connection.getOutputStream();
outStream.write(postDataParams.getBytes());
//Read response
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
//close BufferedReader and OutputStream
br.close();
outStream.close();
//print response
System.out.println("Response : " + strBuilder.toString());
//response converted to JSON object
JSONObject jOb = new JSONObject(strBuilder.toString());
//print JSON object
System.out.println("JSON Object : " + jOb);
//get access token
String access_token = jOb.getString("access_token");
//print access token
System.out.println("Bearer Token : " + access_token);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
}
For test this code first copy this source code and then enter parameters and URL. Now run the Java class. Congratulations! You got a OAuth 2.0 Bearer Access Token. Remember this help and leave a comment. Thank You!
Comments
Post a Comment