1

I want to authenticate Salesforce using Salesforce Rest API with JAVA.

For this I follow the below URL

http://www.asagarwal.com/2401/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java

My java code is below

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.util.EntityUtils;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;

public class OrderProcessing extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static final String PASS = "XXXXXXX";
    static final String SecurityToken = "XXXXXXXXXXXX";
    static final String USERNAME = "[email protected]";
    static final String PASSWORD = PASS + SecurityToken;
    static final String LOGINURL = "https://login.salesforce.com";
    static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
    static final String CLIENTID = "ConsumerKeyFromSalesfoceConnectedApps";
    static final String CLIENTSECRET = "ConsumerSecretFromSalesforceConnectedApps";

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpClient httpclient = HttpClientBuilder.create().build();
        String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET
                + "&username=" + USERNAME + "&password=" + PASSWORD;

        HttpPost httpPost = new HttpPost(loginURL);
        HttpResponse resp = null;

        try {
            resp = httpclient.execute(httpPost);
        } catch (ClientProtocolException cpException) {
            cpException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        final int statusCode = resp.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Error authenticating to Force.com: " + statusCode);
            return;
        }

        String getResult = null;
        try {
            getResult = EntityUtils.toString(resp.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(resp.getStatusLine());
        System.out.println("Successful login");
        System.out.println("  instance URL: " + loginInstanceUrl);
        System.out.println("  access token/session ID: " + loginAccessToken);

        httpPost.releaseConnection();
    }
}

Response : HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Thu, 06 Oct 2016 11:25:26 GMT, Strict-Transport-Security: max-age=10886400; includeSubDomains; preload, Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /_/ContentDomainCSPNoAuth?type=login, Set-Cookie: BrowserId=bIo6TZOBQRSS2KFKtUR5ZA;Path=/;Domain=.salesforce.com;Expires=Mon, 05-Dec-2016 11:25:26 GMT, Expires: Thu, 01 Jan 1970 00:00:00 GMT, Pragma: no-cache, Cache-Control: no-cache, no-store, X-ReadOnlyMode: false, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}}

Response using Rest Web Service Client extension of Chrome : {"error":"invalid_client_id","error_description":"client identifier invalid"}

Please help me to fix this issue.

1
  • was it for individual user or community user ? can we achieve this behavior for community user login ? Commented Sep 20, 2024 at 7:16

3 Answers 3

2

I resolved my Issue using below code :

String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET + "&username=" + USERNAME + "&password=" + PASSWORD;

in place of

String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET
                + "&username=" + USERNAME + "&password=" + PASSWORD;
Sign up to request clarification or add additional context in comments.

Comments

0

Did it work before? If it was then you need auth again.

Comments

0

It takes about 15 minutes for a new Connected App to be all setup. Since the error indicates that the Client ID is bad, I'd guess that is what the issue is. Can you try again?

1 Comment

I resolved this issue using String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET + "&username=" + USERNAME + "&password=" + PASSWORD; But I want to create new user in Salesforce. So how can i do this? Please help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.