0

This is my JAVA code:

package SFtest;



    import java.io.IOException;

        import java.net.*; 

        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.client.HttpClient;
        import org.apache.http.impl.client.HttpClientBuilder;
        import org.apache.http.HttpResponse;
        import org.apache.http.HttpStatus;
        import org.apache.http.StatusLine;
        import org.apache.http.util.EntityUtils;
        import org.apache.http.client.ClientProtocolException;
        import org.json.JSONObject;
        import org.json.JSONTokener;
        import org.json.JSONException;

        public class Main {

            static final String USERNAME     = "******";
            static final String PASSWORD     = "***pwd******secret_key****";
            static final String LOGINURL     = "https://login.salesforce.com";
            static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
            static final String CLIENTID     = "******";
            static final String CLIENTSECRET = "******";

            public static void main(String[] args) {

                HttpClient httpclient = HttpClientBuilder.create().build();

                // Assemble the login request URL
                String loginURL = LOGINURL +
                                  GRANTSERVICE +
                                  "&client_id=" + CLIENTID +
                                  "&client_secret=" + CLIENTSECRET +
                                  "&username=" + USERNAME +
                                  "&password=" + PASSWORD;
                System.out.println("login URL - "+loginURL);

                // Login requests must be POSTs
                HttpPost httpPost = new HttpPost(loginURL);
                HttpResponse response = null;

                try {
                    // Execute the login POST request
                    response = httpclient.execute(httpPost);
                } catch (ClientProtocolException cpException) {
                    System.out.println("Error 1!");
                    cpException.printStackTrace();
                } catch (IOException ioException) {
                    System.out.println("Error 2!");
                    ioException.printStackTrace();
                }

                // verify response is HTTP OK
                final int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    System.out.println("Error authenticating to Force.com: "+statusCode);
                    // Error is in EntityUtils.toString(response.getEntity())
                    return;
                }

                String getResult = null;
                try {
                    getResult = EntityUtils.toString(response.getEntity());
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                JSONObject jsonObject = null;
                String loginAccessToken = null;
                String loginInstanceUrl = null;
                try {
                    jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
                    loginAccessToken = jsonObject.getString("access_token");
                    loginInstanceUrl = jsonObject.getString("instance_url");
                } catch (JSONException jsonException) {
                    jsonException.printStackTrace();
                }
                System.out.println(response.getStatusLine());
                System.out.println("Successful login");
                System.out.println("  instance URL: "+loginInstanceUrl);
                System.out.println("  access token/session ID: "+loginAccessToken);

                // release connection
                httpPost.releaseConnection();
            }
        }

On running this program in eclipse IDE, I'm getting this error:

Error authenticating to Force.com: 400

I read the previous posts related to similar issues, but didn't help in resolving the issue, in my case.

  • 1
    Can you check with Salesforce Admin on what type of OAuth scope he/she has assigned for you to connect – Santanu Boral Jan 10 at 6:59
  • Full Access : scope of authentication in Salesforce. – Monisha S Jan 11 at 6:57
  • Is it a production or sandbox org that you are trying to connect to? – Daniel Ballinger Jan 13 at 22:43
  • Its production. – Monisha S Jan 14 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.