title: Microsoft Graph 401 Unauthorized with access token
body: <p>Unable to get data from the the Microsoft Graph API.</p>
  
  <pre><code>private String getUserNamesFromGraph() throws Exception {
        String bearerToken = "Bearer "+getAccessToken();
        String url = "https://graph.microsoft.com/v1.0/users";
        String returnData = null;
  
        try {
          URL apiURL = new URL(url);
          URLConnection con = apiURL.openConnection();
          con.setRequestProperty("Authorization", bearerToken);
          con.setRequestProperty("Content-Type", "application/json");
  
          BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
              String inputLine;
              StringBuffer response = new StringBuffer();
  
              while((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
  
          returnData = response.toString();
          System.out.println(returnData);
  
        } catch(Exception e) {
          System.out.println(e);
        }
  
        return returnData;
    }
  
  private String getAccessToken() throws Exception {
      String url = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
          URL obj = new URL(url);
          HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  
          // header
          con.setRequestMethod("POST");
          con.setRequestProperty("User-Agent", "eTarget API");
          con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  
          String urlParameters = "client_id=*** 
  APPLICATION ID FROM APPLICATION REGISTRATION PORTAL ***&amp;scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&amp;client_secret=*** 
  APPLICATION SECRET FROM APPLICATION REGISTRATION PORTAL ***&amp;grant_type=client_credentials";
          // Send post request
          con.setDoOutput(true);
          DataOutputStream wr = new DataOutputStream(con.getOutputStream());
          wr.writeBytes(urlParameters);
          wr.flush();
          wr.close();
  
          int responseCode = con.getResponseCode();
          System.out.println("\nSending 'POST' request to URL : " + url);
          System.out.println("Post parameters : " + urlParameters);
          System.out.println("Response Code : " + responseCode);
  
          BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
          in.close();
  
          //print result
      String returnData = response.toString();
          System.out.println(returnData);
  
      Map jsonTokenData = new Gson().fromJson(returnData, Map.class);
      String accessToken = (String)jsonTokenData.get("access_token");
      //System.out.println(accessToken);
  
      return accessToken;
      }
  </code></pre>
  
  <ul>
  <li>The application is <a href="https://apps.dev.microsoft.com" rel="noreferrer">registered</a></li>
  <li>I have a method <code>getAccessToken()</code> that successfully returns an access token</li>
  <li>The method <code>getUserNamesFromGraph()</code> however returns a 401 Unauthorized instead of the expected data.</li>
  </ul>
  
  <p>I've gone through the documentation countless times, trying different variations and endpoints but to no avail. Any ideas appreciated.</p>