I'm trying to upload a file to My One Drive Account using PHP.
But i got this error "Tenant does not have a SPO license."
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `PUT https://graph.microsoft.com/v1.0/users/6290b46e-d76b-444d-9964-23d66beed506/drive/root/children/file.txt/content` resulted in a `400 Bad Request` response: {"error":{"code":"BadRequest","message":"Tenant does not have a SPO license.","innerError"
My Current code is
use Microsoft\Graph\Graph;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Model\EmailAddress;
use \GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
$client = new Client();
$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";
$response = $client->post($tokenEndpoint, [
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'scope' => 'https://graph.microsoft.com/.default',
],
]);
$data = json_decode($response->getBody(), true);
$accessToken = $data['access_token'];
//var_dump($accessToken);
$uploadEndpoint = "https://graph.microsoft.com/v1.0/users/$userId/drive/root/children/file.txt/content";
//$uploadEndpoint = 'https://graph.microsoft.com/v1.0/me/drive/root/children/file.txt/content';
$filePath = '/var/www/html/folder/info.txt';
$response = $client->put($uploadEndpoint, [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'text/plain', // Change the content type as needed
],
'body' => fopen($filePath, 'r'), // Read the file contents
]);
// Check the response
if ($response->getStatusCode() == 201) {
echo 'File uploaded successfully!';
} else {
echo 'File upload failed. Status Code: ' . $response->getStatusCode();
echo 'Response: ' . $response->getBody();
}
Can anyone Provide me the solution for that how to resolve this issue.
The error "Tenant does not have a SPO license" usually occurs if the tenant does not have the required licenses to perform the action.
To resolve the error, make sure to add Office 365 license to your tenant.
I generated access token by using below parameters via postman:
For sample, using the above access token I am able to upload the file to OneDrive successfully:
If still the issue persists, check this Microsoft QnA by CarlZhao-MSFT.
Reference:
microsoft graph api - Tenant does not have a SPO license - Stack Overflow by Dan Kershaw - MSFT