0

I am trying to make Connect Salesforce with Java using Rest Api. But I am getting authentication Error and status = 400 I followed the all the steps mentioned here : http://resources.docs.salesforce.com/204/17/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf

Below is my Java Code:

public class Main {

    static final String USERNAME     = "tanujsfdcfsp@gmail.com";
    static final String PASSWORD     = "******************";
    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) {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        // Assemble the login request URL
        String loginURL = LOGINURL + 
                          GRANTSERVICE + 
                          "&client_id=" + CLIENTID + 
                          "&client_secret=" + CLIENTSECRET +
                          "&username=" + USERNAME +
                          "&password=" + PASSWORD;

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

        try {
            // Execute the login POST request
            response = httpclient.execute(httpPost);
        } catch (ClientProtocolException cpException) {
            System.out.println("Exception: "+cpException);
            // Handle protocol exception
        } catch (IOException ioException) {
            // Handle system IO exception
        }

        // 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) {
            // Handle system IO exception
        }
        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) {
            // Handle JSON exception
        }
        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();
    }
}
  • Have you checked, whether you are able to connect through any REST client like POSTMAN to make sure it is a JAVA issue? – Saroj Bera Dec 16 '16 at 5:32
  • Did you try by appending security token along with password? – Reshma Dec 16 '16 at 8:22
0

Please use oAuth Authentication, Please refer the following link - https://developer.salesforce.com/page/Use_Salesforce_REST_API_with_Your_JavaServer_Pages

In My scenario - From my java application i was fetching all reports display all those reports in jsp using j2ee architecture.

  • He is already using OAuth. This is called Username-Password OAuth Authentication Flow. I think he is using it to avoid user interaction as it is a standalone class. – Saroj Bera Dec 16 '16 at 9:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.