1

I have developer account on salesforce.com.

Using Java, I am trying to connect, but I am getting a 400 error. I tried to pass a token as well as without.

package src;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

class Main{
    static final String USERNAME     = "************";
    static final String PASSWORD     = "pass+token";
    static final String LOGINURL     = "https://login.salesforce.com";
    static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password&access_token=offline";
    static final String CLIENTID     = "xxxx";
    static final String CLIENTSECRET = "xxxx";
    private static String REST_ENDPOINT = "/services/data" ;
    private static String API_VERSION = "/v41.0" ;
    private static String baseUri;
    private static Header oauthHeader;
    private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");

    static String Name="basic.txt";
    static String AttachmentId="";


    public static void main(String[] args) {

        @SuppressWarnings("deprecation")
        DefaultHttpClient httpclient = new DefaultHttpClient();


        String loginURL = LOGINURL +

                          GRANTSERVICE +

                          "&client_id=" + CLIENTID +

                          "&client_secret=" + CLIENTSECRET +

                          "&username=" + USERNAME +

                          "&password=" + PASSWORD;


        System.out.println("login URL:"+loginURL);

        HttpPost httpPost = new HttpPost(loginURL);

        HttpResponse response = null;

        try {


            response = httpclient.execute(httpPost);

        } catch (ClientProtocolException cpException) {

            System.out.println("in client protocol exception:"+cpException);

        } catch (IOException ioException) {

            System.out.println("in ioexception:"+ioException);


}



        final int statusCode =

response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {

            System.out.println("Error authenticating to Force.com:"+statusCode);


return; }

        String getResult = null;

        try {

            getResult = EntityUtils.toString(response.getEntity());

        } catch (IOException ioException) {


        }

        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) {


        }

        System.out.println(response.getStatusLine());

        System.out.println("Successful login");

        System.out.println("  instance URL: "+loginInstanceUrl);

        System.out.println("  access token/session ID:"+loginAccessToken);


        httpPost.releaseConnection();

    }
}

Do I have to make my own instance like: www.myinstance.salesforce.com?

4
  • Please, never share clientId's and Secrets in your posts. I strongly recommend you reset them asap. I have removed them from your post. Thanks – glls Nov 2 '17 at 14:57
  • You need to pass Grant_type and other parameters as a request body, you are passing them as a URL param, that might be causing issue – Prasanna Nov 3 '17 at 6:30
  • @Prasanna can you explain me a little bit more? – Blagoje Stankovic Nov 3 '17 at 9:56
  • @BlagojeStankovic Have you added HTTPClient jar? I think you shouldn't be using DefaultHTTPClient. It should be like HttpClient httpclient = HttpClientBuilder.create().build(); – Prasanna Nov 6 '17 at 14:36

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.