Reputation: 491
I tried using the library from krizalys 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.
Since I read that the Live SDK used in krizalys example will be offline soon (as mentioned here), I tried implementing Microsoft Graph instead.
I implemented two ways to get an access token at the moment, one with grant type password
(getAccessToken Method from this sample used) and one with client_credentials
(Like in the krizalys lib). Both seem to work and return an access_token
and refresh_token
, but when I try to make a request I get the message:
"InvalidAuthenticationToken [message] => Access token is empty"
The code for my request:
$data = array("name" => "test");
$url = "https://graph.microsoft.com/v1.0/me/drive/root";
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $url, [
'headers' => [
'Authorization: Bearer ' . $this->_state->token->data->access_token,
'Content-Type: application/json',
'Content-Length: ' .strlen(json_encode($data))
],
'body' => json_encode($data),
]);
I also tried it with the GET method and added the Host: graph.microsoft.com
to ensure that this is not the problem:
$url = "https://graph.microsoft.com/v1.0/me";
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $url, [
'headers' => [
'Authorization: Bearer ' . $this->_state->token->data->access_token,
'Host: graph.microsoft.com'
],
]);
The token response payload looks like this:
The application is configured at https://apps.dev.microsoft.com and the permissions are set. Is there anything wrong with my request? I have no idea why I always get the InvalidAuthenticationToken
message. Thanks.
Upvotes: 3
Views: 3816
Reputation: 33122
You've registered your application in the v2 Endpoint (apps.dev.microsoft.com
) but the sample code you're using is for the v1 Endpoint. These are not interchangeable. Also, password
isn't a valid OAuth Grant for the v2 Endpoint (v2 supports authorization_code
, implicit
, and client_credentials
)
You need to obtain your token from the v2 Endpoint. You might find these articles helpful:
Upvotes: 1