locked
HTTP 400 error when trying to get token RRS feed

  • Question

  • Hi,

    Hope someone can help me with an issue that I have been having for the past few days. I am trying to get an integration working with Dynamics using the Web API. The code I have is java. 

    I setup a trial for Dynamics to test against and my code works fine, however, not that I am trying to integrate using an actual customers details I am getting a 400 error while trying to get a token.

    Comparing the customers version to the trial I can see that the customers is:

    Microsoft Dynamics® CRM Online 2016 Update(8.1.0.549) (DB 8.1.0.538)

    Whereas the trial version is 

    Microsoft Dynamics 365Version 1612 (8.2.0.764) (DB 8.2.0.764) online

    Apart from the version the only other difference I have seen is that the customer has different domains in their Azure Directory. I am not sure what the problem could be, from the reading I have done I got the impression that the API should work with the version they are on - I dont know if it requires extra information in the request.

    My authentication code is as follows:

    HttpURLConnection connection = null;

     Map<String,Object> params = new LinkedHashMap<>();
     params.put("client_id", CLIENT_ID);
     params.put("resource", RESOURCE);
     params.put("username", USERNAME);
     params.put("password", PASSWORD);
     params.put("grant_type", "password");
     params.put("client_secret", SECRET_KEY);

     StringBuilder postData = new StringBuilder();
           for (Map.Entry<String,Object> param : params.entrySet()) {
               if (postData.length() != 0) postData.append('&');
               postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
               postData.append('=');
               postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
           }
           byte[] postDataBytes = postData.toString().getBytes("UTF-8");
          int b = postDataBytes.length;

     URL url = new URL(AUTHORITY);
           connection = (HttpURLConnection) url.openConnection();
           connection.setUseCaches(false);
           connection.setDoOutput(true);
           connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
           connection.setRequestProperty("Cache-Control", "no-cache");
           connection.setRequestMethod("POST" );
           connection.setRequestProperty("OData-MaxVersion", "4.0");
           connection.setRequestProperty("OData-Version", "4.0");
           connection.setRequestProperty("Accept", "application/json");
           connection.setRequestProperty("Content-Length", String.valueOf(b));
           connection.getOutputStream().write(postDataBytes);
          
           Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

           
           StringBuilder sb = new StringBuilder();
           for (int c; (c = in.read()) >= 0;)
               sb.append((char)c);
           String response = sb.toString();
       
           Object jResponse;
           jResponse = JSONValue.parse(response.toString());
           JSONObject jObject = (JSONObject) jResponse;
          String token = jObject.get("access_token").toString();
           
           return token;

    Any help would be greatly appreciated.

    Tuesday, January 3, 2017 1:49 PM

All replies

  • Used the postman browser plugin and see to get more error information there 

    {
        "error": "invalid_grant",
        "error_description": "AADSTS65001: The user or administrator has not consented to use the application with ID 'xxxxxxxxxxx'. Send an interactive authorization request for this user and resource.

    From reading around it seems as though my user details must not be for an admin, and I need an admin to approve the app registration. Still waiting to confirm but seems to be my best bet from what I can see

    Tuesday, January 3, 2017 2:49 PM