我正在尝试向 Microsoft Dynamics CRM REST API 发出请求。 我收到 403 响应,其中包含消息“该用户不是组织的成员”。 谁能告诉我哪里出错了?
我正在使用的Java代码(我已经删除了id/主机名等):
public class CRMTesting {
private static String TENANT_ID = "<tenant-id>";
private static String POST_URL = "https://login.microsoftonline.com/" + TENANT_ID + "/oauth2/token";
private static String CLIENT_ID = "<client-id>";
private static String CLIENT_SECRET = "<client-secret>";
public static void main(String[] args) throws Exception {
String token = getToken();
getAccounts(token);
}
public static String getAccounts(String token) throws MalformedURLException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://<company>.crm4.dynamics.com/api/data/v8.2/accounts?$select=name&$top=3");
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Authorization", "Bearer " + token);
httpGet.setHeader("OData-MaxVersion", "4.0");
httpGet.setHeader("OData-Version", "4.0");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
System.out.println("Response Status Code... " + httpResponse.getStatusLine().getStatusCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
httpClient.close();
System.out.println(response.toString());
// Extract the token from the json response
final ObjectMapper mapper = new ObjectMapper();
final JsonNode actualObj = mapper.readTree(response.toString());
return actualObj.get("access_token").asText();
}
public static String getToken() throws MalformedURLException, IOException {
// create headers
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(POST_URL);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("grant_type", "client_credentials"));
nvps.add(new BasicNameValuePair("resource", "https://<company>.crm4.dynamics.com"));
nvps.add(new BasicNameValuePair("client_id", CLIENT_ID));
nvps.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
httpClient.close();
System.out.println(response.toString());
// Extract the token from the json response
final ObjectMapper mapper = new ObjectMapper();
final JsonNode actualObj = mapper.readTree(response.toString());
return actualObj.get("access_token").asText();
}
}
403响应:
{"token_type":"Bearer","expires_in":"3599","ext_expires_in":"3599","expires_on":"1698847590","not_before":"1698843690","resource":"https://<company>.crm4.dynamics.com","access_token":"<access-token>"}
Response Status Code... 403
{"error":{"code":"0x80072560","message":"The user is not a member of the organization."}}
我尝试了很多事情,例如在令牌请求中添加“范围”参数..使用不同的POST url“/oauth2/v2.0/token”..使用java.net.HttpURLConnection而不是apache CloseableHttpClient..等.但是我还没有找到解决方案。
任何帮助都会很棒!
谢谢