• Rhythm Arora
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Here is my code:
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
    String access="";
    String data = "";
    public static void main(String[] args) throws Exception {
        HttpURLConnectionExample http = new HttpURLConnectionExample();
            
        http.sendPost();
    }
    
    // HTTP POST request
    private void sendPost() throws Exception {
        String url = "https://login.salesforce.com/services/oauth2/token";
        URL obj = new URL(url);
        HttpsURLConnection con1 = (HttpsURLConnection) obj.openConnection();
        //add reuqest header
        con1.setRequestMethod("POST");
         con1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        String urlParameters =    "client_id=" + URLEncoder.encode("****************************************", "UTF-8")+
                "&client_secret=" + URLEncoder.encode("******", "UTF-8")+
                "&grant_type=" + URLEncoder.encode("authorization_code", "UTF-8")+
                "&code=" + URLEncoder.encode("***********", "UTF-8")+
                "&redirect_uri=" + URLEncoder.encode("*******.co.in", "UTF-8");
        // Send post request
        con1.setDoOutput(true);
        try    {
            OutputStream w=con1.getOutputStream();
            DataOutputStream wr = new DataOutputStream(w);
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
        }
        catch(Exception e){
            System.out.println("Exception test" + e);
        }
        
        int responseCode = con1.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(con1.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        access=response.substring(17,129);
        System.out.println(access);
    }
    
}
 
 
//Error Message:
Response Code : 400
 
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://login.salesforce.com/services/oauth2/token
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at org.json.simple.HttpURLConnectionExample.sendPost(HttpURLConnectionExample.java:109)
    at org.json.simple.HttpURLConnectionExample.main(HttpURLConnectionExample.java:26)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://login.salesforce.com/services/oauth2/token
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at org.json.simple.HttpURLConnectionExample.sendPost(HttpURLConnectionExample.java:101)
    ... 1 more
 

 
I will really appreciate it if you can help me this out.
 
Thanks
Rhythm Arora