1

I tested the following code and is working perfectly on sandbox and returning the following:

HTTP/1.1 200 OK
Successful login
  instance URL: https://XXXX.salesforce.com
  access token/session ID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Whenever I set the configuration to production and try to run the same code I get the following:

Error authenticating to Force.com: 301

I double checked the username, password and everything but it still fails.

Below is my java code:

I run it as a jar same for sandbox and production while changing the configuration file which has the variables USERNAME, PASSWORD, LOGINURL, etc.

void getInstanceAndAccessToken() {

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

        // 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);
        HttpResponse response = null;

        try {
            // Execute the login POST request
            response = httpclient.execute(httpPost);
        } catch (ClientProtocolException cpException) {
            cpException.printStackTrace();
        } catch (IOException ioException) {
            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();
}
  • Is your client id/secret still from sandbox or it points to production? – Jayant Das May 20 '19 at 17:15
  • 3
    301 is a redirect, not an error. Please make sure you are going to the correct LOGINURL when logging in. If a redirect occurs, that means you went to the wrong URL. Also, you might want to debug the response value in order to get more information. – sfdcfox May 20 '19 at 17:36
0
  1. Make sure your connected app is set up in Production, its correctly provisioned and permissioned and client_id/client_secret values are correct.

  2. Password should contain the security token

  3. Login URL and grant_type parameter are correct.

Take a look at the docs, they spell this out. If your authentication use case is service-to-service (no user interaction), you should take a look at JWT Bearer flow.

| improve this answer | |

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.