Строка не может быть приведена к org.json.JSONObject при входе в систему salesforce rest api - PullRequest
0 голосов
/ 28 августа 2018

Я пытаюсь установить соединение через зависимости Java и Maven:

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

, просмотрев этот урок на YouTube: https://www.youtube.com/watch?v=E8bA4uNyX-M

код выполняет вход в отдел продаж:

public class Main {

static final String USERNAME = "myUserName@gmail.com";
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();       
    }

}

Вот полный текст исключения:

URL для входа: https://salesForceURL.aspx‏/services/oauth2/token?grant_type=password&client_id=3MVG954MqIw6Qn8lEZgJaD5xiAePJP79Ra6TYJ60QlYK4BKx_IJJQBnxzkTzpR5q5CYGJU1b&client_secret=21689523032&username=myUserName@gmail.com&password=myPassword12345 Исключение в потоке "main" java.lang.ClassCastException: java.lang.String не может быть приведено к org.json.JSONObject. в SalesForceRest.SalesForceRest.Main.main (Main.java:65)

Что я не правильно делаю?

1 Ответ

0 голосов
/ 28 августа 2018

Вместо приведения к JSONObject создайте объект, используя String:

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

Вы также можете построить JSONObject, используя JSONTokener :

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