0
votes

I try to establish connection via Java and Maven dependencies:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

by watching this tutorial in YouTube: https://www.youtube.com/watch?v=E8bA4uNyX-M

code performs login to sales force:

public class Main {

static final String USERNAME = "[email protected]";
static final String PASSWORD = "myPassword12345";
static final String LOGINURL = "https://salesForceURL.aspx‏";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";

    public static void main(String[] args) {
        HttpClient httpclient = HttpClientBuilder.create().build();
        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 e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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 (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.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();       
    }

}

Here's the full exception text:

Login URL: https://salesForceURL.aspx‏/services/oauth2/token?grant_type=password&client_id=3MVG954MqIw6Qn8lEZgJaD5xiAePJP79Ra6TYJ60QlYK4BKx_IJJQBnxzkTzpR5q5CYGJU1b&client_secret=21689523032&[email protected]&password=myPassword12345 Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject at SalesForceRest.SalesForceRest.Main.main(Main.java:65)

What I'm not doing right?

1
----> Main.java:65fantaghirocco
Thanks Haim I did paste not really my tokenHaim Sabag

1 Answers

0
votes

Instead of casting to JSONObject, create an object using String:

new JSONObject(new JSONTokener(getResult).nextValue());

You can also construct JSONObject using JSONTokener:

new JSONObject(new JSONTokener(getResult));