我有SugarCRM跟踪帐户。我可以通过以下网址获取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 API从CRM中检索Leads我无法找到合适的方法和Url来做这件事。
我可以从/ help看到API的列表其余部分但是我无法理解我的模块名称应该是什么以及我对:记录的内容以及如何通过我的访问令牌用于身份验证。
有人可以帮助我吗?
答案 0 :(得分:2)
模块名称只是您从中获取记录的模块,因此在您的情况下,您需要执行GET请求以休息/ v10 / Leads以获取潜在客户列表。如果您想获取特定的潜在客户,您可以替换:使用潜在客户的ID记录 - 例如:GET rest / v10 / Leads / LEAD-ID-HERE
SugarCRM的文档中包含许多可能未包含在/ help和工作示例中的相关信息。
答案 1 :(得分:2)
您需要将检索到的令牌包含在OAuth-Token标头中以用于后续请求,然后只需将模块名称用作端点,即在您的情况下:" 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 }"