title: OneDrive file access with php using microsoft graph
body: <p>I tried using <a href="https://github.com/krizalys/onedrive-php-sdk" rel="nofollow noreferrer">the library from krizalys</a> for an implementation to read and write files from OneDrive. It should work for business accounts but would be nice if it could also work for personal accounts. </p>
  
  <p>Since I read that the Live SDK used in krizalys example will be offline soon (<a href="https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/migrating-from-live-sdk" rel="nofollow noreferrer">as mentioned here</a>), I tried implementing Microsoft Graph instead.</p>
  
  <p>I implemented two ways to get an access token at the moment, one with grant type <code>password</code> (<a href="https://github.com/microsoftgraph/php-connect-rest-sample/blob/master/tests/IntegrationTest.php" rel="nofollow noreferrer">getAccessToken Method from this sample used</a>) and one with <code>client_credentials</code> (Like in the <a href="https://github.com/krizalys/onedrive-php-sdk" rel="nofollow noreferrer">krizalys lib</a>). Both seem to work and return an <code>access_token</code> and <code>refresh_token</code>, but when I try to make a request I get the message:</p>
  
  <blockquote>
    <p>"InvalidAuthenticationToken [message] => Access token is empty"</p>
  </blockquote>
  
  <p>The code for my request:</p>
  
  <pre><code>$data = array("name" =&gt; "test");
  $url  = "https://graph.microsoft.com/v1.0/me/drive/root";
  $client = new \GuzzleHttp\Client();
  $response = $client-&gt;request('POST', $url, [
    'headers' =&gt; [
      'Authorization: Bearer ' . $this-&gt;_state-&gt;token-&gt;data-&gt;access_token,
      'Content-Type: application/json',
      'Content-Length: ' .strlen(json_encode($data))
     ],
     'body' =&gt; json_encode($data),
  ]);
  </code></pre>
  
  <p>I also tried it with the GET method and added the <code>Host: graph.microsoft.com</code> to ensure that this is not the problem:</p>
  
  <pre><code>$url = "https://graph.microsoft.com/v1.0/me";
  $client = new \GuzzleHttp\Client();
  $response = $client-&gt;request('GET', $url, [
    'headers' =&gt; [
      'Authorization: Bearer ' . $this-&gt;_state-&gt;token-&gt;data-&gt;access_token,
      'Host: graph.microsoft.com'
    ],
  ]);
  </code></pre>
  
  <p>The token response payload looks like this:
  <a href="https://i.stack.imgur.com/xJSyv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xJSyv.png" alt="enter image description here"></a></p>
  
  <p>The application is configured at <a href="https://apps.dev.microsoft.com" rel="nofollow noreferrer">https://apps.dev.microsoft.com</a> and the permissions are set. Is there anything wrong with my request? I have no idea why I always get the <code>InvalidAuthenticationToken</code> message. Thanks.</p>