'Cannot request microsoft access token for OneDrive file upload within PHP
I need some help. Since 2 days, I'm reading several tutorials and trying out code snippets I've found to get an access token from Microsoft to upload a single small file from PHP to OneDrive. This is my current code:
$onedrive_tenant_id = 'f8cdef31-a31e-4b4a-93e4-5f571e91255a';
$onedrive_client_id = '2947eb86-xxxxxx....';
$onedrive_client_secret = '6994277d-xxxxxx....';
if ( ! empty( $onedrive_tenant_id ) && ! empty( $onedrive_client_id ) && ! empty( $onedrive_client_secret ) ) {
$guzzle = new Client();
$url = 'https://login.microsoftonline.com/' . $onedrive_tenant_id . '/oauth2/token';
$user_token = json_decode( $guzzle->post( $url, [
'form_params' => [
'client_id' => $onedrive_client_id,
'client_secret' => $onedrive_client_secret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'password',
'username' => '[email protected]',
'password' => '123456789',
'scope' => 'https://graph.microsoft.com/Files.ReadWrite'
],
] )->getBody()->getContents() );
}
But its not working... Every time I execute the script, I'm getting different exceptions. I've ended up with this one:
PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error:
POST https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/oauth2/token
resulted in a400 Bad Request
response: {"error":"invalid_request","error_description":"AADSTS90002: Tenant 'web.de' not found. Check to make sure you have the (truncated...)
I don't understand why it's so complicated to just upload a file to OneDrive via PHP.
This is the tutorial I was following: https://www.webshark.ee/how-to-upload-files-from-server-to-microsoft-onedrive-using-rest-api-and-php/
Solution 1:[1]
The suggestable procedure is to use msgraph-sdk-php
for this procedure and there is a complete clue that you are using the same but need to have few more extensions.
Create a sample application with PHP web service. Must note down that we need to have open access office 365 account with one drive support. (https://github.com/AI-EVO/PHP-MS-Graph)
Check this readme tutorial from MSGRAPH-SDK-PHP
Register designed application in step 1 with ADD through Graph API (https://github.com/microsoftgraph/msgraph-sdk-php#register-your-application)
Create your permissions list and check with availability and accessibility of those.
For further reference use the link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SairamTadepalli-MT |