我正在尝试使用PHP上传文件到My One Drive帐户。
但是我得到了这个错误"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"
字符串
我的当前代码是
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();
}
型
任何人都可以提供我的解决方案,如何解决这个问题。
1条答案
按热度按时间kcrjzv8t1#
如果租户没有执行操作所需的许可证,则通常会出现错误 “租户没有SPO许可证”。
若要解决此错误,请确保将Office 365许可证添加到您的租户。
x1c 0d1x的数据
我通过postman使用以下参数生成了access token:
字符串
的
对于sample,使用上述访问令牌,我可以成功将文件上传到OneDrive:
型
的
的
参考:
microsoft graph api - Tenant does not have a SPO license - Stack Overflow作者:Dan Kershaw