azure 我有一个错误,当我试图从我的系统上传文件到一个驱动器使用PHP

nnvyjq4y  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(163)

我正在尝试使用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();
}


任何人都可以提供我的解决方案,如何解决这个问题。

kcrjzv8t

kcrjzv8t1#

如果租户没有执行操作所需的许可证,则通常会出现错误 “租户没有SPO许可证”
若要解决此错误,请确保将Office 365许可证添加到您的租户。
x1c 0d1x的数据
我通过postman使用以下参数生成了access token

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
scope:https://graph.microsoft.com/.default
grant_type:client_credentials

字符串



对于sample,使用上述访问令牌,我可以成功将文件上传到OneDrive:

https://graph.microsoft.com/v1.0/users/[email protected]/drive/root:/test.txt:/content
Content-Type: text/plain 

The contents of the file



  • 如果问题仍然存在 *,请通过CarlZhao-MSFT检查此Microsoft QnA
    参考:

microsoft graph api - Tenant does not have a SPO license - Stack Overflow作者:Dan Kershaw

相关问题