title: 401 Unauthorized when try to call office 365 API with an obtained access token
body: <p>I'm using Java to create a simple daemon or service app (not Web app) to call an Office 365 Calendar API. I've followed this guide <a href="https://developer.microsoft.com/en-us/graph/docs/authorization/app_only" rel="nofollow noreferrer">Call Microsoft Graph in a service or daemon app</a>, but when I try to call the API with the access token, I obtain a 401 error.
  I've registered the app to azure portal with all the Graph authorizations and I've made the certificate, following the Step 1 of this guide: <a href="http://www.nubo.eu/en/blog/2016/11/step-by-step-guide-on-how-to-create-a-deamon-application-for-office365/" rel="nofollow noreferrer">Get a certificate or create a self-signed certificate</a>. Here is my code for the access token request:`</p>
  
  <pre><code>    String accessToken="";
      String token_endpoint = "https://login.windows.net/&lt;mytenant&gt;/oauth2/token";
      String grant_type = "client_credentials";
      String client_secret = &lt;mysecret&gt;;
      String resource = "https://graph.microsoft.com";
      String client_id = &lt;myclient&gt;;
  
      CloseableHttpClient httpclient = HttpClients.createDefault();
      HttpPost httpPost = new HttpPost(token_endpoint);
      List&lt;NameValuePair&gt; parameters = new ArrayList&lt;&gt;();
      parameters.add(new BasicNameValuePair("grant_type", grant_type));
      parameters.add(new BasicNameValuePair("client_id", client_id));
      parameters.add(new BasicNameValuePair("client_secret", client_secret));
      parameters.add(new BasicNameValuePair("resource", resource));
      httpPost.setEntity(new UrlEncodedFormEntity(parameters));
      httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
  
      try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
          System.out.println(response.getStatusLine());
          HttpEntity entity = response.getEntity();
  
          //parsing
          JSONParser parser = new JSONParser();
          Scanner httpResponseScanner = new Scanner(entity.getContent());
          String jsonString = httpResponseScanner.nextLine();
          //System.out.println(jsonString);
          JSONObject json = (JSONObject) parser.parse(jsonString);
          accessToken = json.get("access_token").toString();
  
          EntityUtils.consume(entity);
      }
      return accessToken;`
  </code></pre>
  
  <p>And here is my API call code:`</p>
  
  <pre><code>     String apiURL = "https://outlook.office.com/api/v2.0/me/calendars";
       CloseableHttpClient httpclient = HttpClients.createDefault();
       HttpGet httpGet = new HttpGet(apiURL);
       httpGet.addHeader("Accept", "application/json");
       httpGet.addHeader("Authorization", "Bearer " + accessToken);
  
       try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
           System.out.println(response.getStatusLine());
           HttpEntity entity = response.getEntity();
           EntityUtils.consume(entity);
       }`
  </code></pre>
  
  <p>I've already tested my access token to jwt.io with invalid signature response, so I think there is something wrong in my token request. Someone can help me?</p>