Error Response : Bad Requestimport javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OauthAuthentication {
private static final Pattern pat = Pattern.compile(".*\"access_token\"\\s*:\\s*\"([^\"]+)\".*");
private static final String clientId = "jdiwiweoiwe.NxkdgEZTQHC3SvdVQyFm5eSmbiDCQ";//clientId
private static final String clientSecret = "fhdkhskjs";//client secret
private static final String tokenUrl = 'https://login.salesforce.com/services/oauth2/token";
private static final String auth = clientId + ":" + clientSecret;
private static final String authentication = Base64.getEncoder().encodeToString(auth.getBytes());
private static String userName="xyz";
private static String password="abc";
public static void main(String[] args) throws IOException
{
//Username is netId for the person making the call and the password is there password
String content = "grant_type=password&username=" + userName + "&password=" + password;
BufferedReader reader = null;
HttpsURLConnection connection = null;
String returnValue = "";
try {
System.out.println("authentication"+authentication);
URL url = new URL(tokenUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authentication);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
PrintStream os = new PrintStream(connection.getOutputStream());
os.print(content);
os.close();
System.out.println("content"+content);
/*reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
StringWriter out = new StringWriter(connection.getContentLength() > 0 ? connection.getContentLength() : 2048);
while ((line = reader.readLine()) != null) {
out.append(line);
}
String response = out.toString();
System.out.println("output="+response);
Matcher matcher = pat.matcher(response);
if (matcher.matches() && matcher.groupCount() > 0) {
returnValue = matcher.group(1);
}*/
System.out.println("response : " + connection.getResponseMessage());
} catch (Exception e)
{
System.out.println("Error : " + e.getMessage()+"\n"+e.getCause()+"\n"+e.getStackTrace()+"\n"+e.getLocalizedMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
connection.disconnect();
}
}
}
Hi,Check below links that can help you.https://developer.okta.com/blog/2019/10/30/java-oauth2https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_auth_oauth.htmhttps://developer.salesforce.com/forums/?id=906F00000008sNHIAYhttps://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/create_java_client_oauth.htm⌗:~:text=Run%20a%20Java%20client%20that,the%20channel%20with%20OAuth%20authentication.&text=Obtain%20an%20OAuth%20bearer%20access,token%20in%20the%20connector%20example.Thanks,Vinay Kumar