登录 salesforce rest api 时,无法将字符串转换为 org.json.JSONObject

String cannot be cast to org.json.JSONObject when login salesforce rest api

我尝试通过 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=密码&client_id=3MVG954MqIw6Qn8lEZgJaD5xiAePJP79Ra6TYJ60QlYK4BKx_IJJQBnxzkTzpR5q5CYGJU1b&client_secret=21689523032&用户名=我的用户名@gmail.com&密码=我的密码12345 线程 "main" java.lang.ClassCastException 中的异常:java.lang.String 无法转换为 org.json.JSONObject 在 SalesForceRest.SalesForceRest.Main.main(Main.java:65)

我做错了什么?

不是强制转换为 JSONObject,而是使用 String:

创建一个对象
new JSONObject(new JSONTokener(getResult).nextValue());

您还可以使用 JSONTokener:

构造 JSONObject
new JSONObject(new JSONTokener(getResult));