0

I am trying to authenticate my java client as explained in this link: https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_auth_oauth.htm

I have created th connectedApp but I did not give any CallBackURL, thinking that it's not mandatory if I am just doing a POST request to get Access token.

I get 400 bad request error. Is it mandatory to set the callback url in connected app, when trying to just get the auth token? Ican do authentication as well as GET request using the access token using Postman. My code is as below.

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.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class RestAPIClient2 {

    static final String LOGINURL     = "https://login.salesforce.com";
    static final String GRANTSERVICE = "/services/oauth2/tokengrant_type=password";
    private static final String CLIENTID = "*******";
    private static final String CLIENTSECRET = "----25284058416";
    private static final String USERID = "****userID.com";
    private static final String PASSWORD = "*****"; //password + securityToken

    private static final String ACCESSTOKEN = "access_token";
    private static final String INSTANCEURL = "instance_url";
    private static String instanceUrl;
    private static Header oAuthHeader;

    public static void main(String[] args) {
    HttpClient httpclient = HttpClientBuilder.create().build();

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

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

        try {
            httpResponse = httpclient.execute(httpPost);
        } catch (ClientProtocolException clientProtocolException) {
            clientProtocolException.printStackTrace();
        } catch (IOException ioException) {
                        ioException.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        final int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Error:  " + statusCode + "\t\t "+ httpResponse.getStatusLine().getReasonPhrase());
            System.out.println("Error authenticating to Salesforce.com platform: " + statusCode);
            return;
        }

        String httpMessage = null;
        try {
            httpMessage = EntityUtils.toString(httpResponse.getEntity());
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        JSONObject jsonObject = null;
        String accessToken = null;
        try {
            jsonObject = (JSONObject) new JSONTokener(httpMessage).nextValue();
            accessToken = jsonObject.getString(ACCESSTOKEN);
            instanceUrl = jsonObject.getString(INSTANCEURL);
        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }

        //oAuthHeader = new BasicHeader("Authorization", "OAuth2 " + accessToken);
        httpPost.releaseConnection();
    }


    }
  • can you provide the relevant part of the code related to the status code, the body of the post and headers you are sending for example. No tokens please. – glls Feb 2 '18 at 1:46
  • No, it seems like something else is going on. All the documentation says you don't need it. – sfdcfox Feb 2 '18 at 3:01
  • @glls, I have edited my question with the code. – Quest_sfdc Feb 2 '18 at 19:17
  • 1
    GRANTSERVICE looks odd, aren't you missing a ? before grant_type=password ? – cropredy Feb 4 '18 at 19:24
  • @cropredy That's likely the issue. tokengrant_type=password"; should be token?grant_type=password"; – TheSFDCTower Feb 4 '18 at 21:38

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.