从 SUGARCRM 查询数据

Querying Data from SUGARCRM

我有 SugarCRM 跟踪帐户。我可以通过以下 url.

获得 Authenticate 和 AccessToken
https://xxxxxxx.trial.sugarcrm.eu/rest/v10/oauth2/token

方法:POST POST 数据:postData:{ "grant_type":"password", "client_id":"sugar", "client_secret":"", "username":"admin", "password":"Admin123", "platform":"base" }

我用来获取AccessToken的代码

public static String getAccessToken() throws JSONException {
    HttpURLConnection connection = null;

    JSONObject requestBody = new JSONObject();
    requestBody.put("grant_type", "password");
    requestBody.put("client_id", CLIENT_ID);
    requestBody.put("client_secret", CLIENT_SECRET);
    requestBody.put("username", USERNAME);
    requestBody.put("password", PASSWORD);
    requestBody.put("platform", "base");

    try {
        URL url = new URL(HOST_URL + AUTH_URL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.connect();

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        out.write(requestBody.toString());
        out.close();

        int responseCode = connection.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject jObject = new JSONObject(response.toString());
        if(!jObject.has("access_token")){
            return null;
        }
        String accessToken = jObject.getString("access_token");

        return accessToken;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

现在我可以使用 rest 从 CRM 检索潜在客户 API 我找不到合适的方法,Url 无法完成这件事。

我可以从 /help 中看到 API 的其余列表,但我不明白我的 模块 名称应该是什么以及我必须 :record 以及如何传递我的访问令牌进行身份验证。

谁能帮帮我?

模块名称就是您要从中获取记录的模块,因此在您的情况下,您需要向 rest/v10/Leads 发出 GET 请求以获取潜在客户列表。如果您想获取特定的潜在客户,请将 :record 替换为潜在客户的 ID - 例如:GET rest/v10/Leads/LEAD-ID-HERE

SugarCRM 的文档有很多可能未包含在 /help 和工作示例中的相关信息。

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/Integration/Web_Services/v10/Endpoints/module_GET/

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/Integration/Web_Services/v10/Examples/PHP/How_to_Fetch_Related_Records/

您需要将检索到的令牌包含到 OAuth-Token header 中以用于后续请求,然后只需使用模块名称作为端点,即在您的情况下:"rest/v10/Leads" 并调用GET 方法来检索它们。尝试类似于此的操作:

    String token = getAccessToken();
    HttpURLConnection connection = null;
    try {

        URL url = new URL(HOST_URL + "/rest/v10/Leads");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("OAuth-Token", token);
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.connect();


        int responseCode = connection.getResponseCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject jObject = new JSONObject(response.toString());

        System.out.println(jObject);


    } catch (Exception e) {
        e.printStackTrace();
    }

如果您想将其过滤到特定的 ID 以减少返回的数据量,您可以在模块名称后指定它,即 "rest/v10/Leads/{Id}"