提问人:Prits 提问时间:11/10/2023 更新时间:11/20/2023 访问量:75
当我尝试使用 PHP 将文件从我的系统上传到 ONE Drive 时,我遇到了错误
I have an error when i tried to upload a file from my system to ONE Drive using PHP
问:
我正在尝试使用 PHP 将文件上传到我的 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();
}
谁能为我提供如何解决此问题的解决方案。
答:
0赞
Rukmini
11/20/2023
#1
如果租户没有执行该操作所需的许可证,通常会出现错误“租户没有 SPO 许可证”。
若要解决此错误,请确保将 Office 365 许可证添加到租户。
我通过邮递员使用以下参数生成了访问令牌:
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
例如,使用上述访问令牌,我能够成功将文件上传到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 - 租户没有 SPO 许可证 - Stack Overflow 作者:Dan Kershaw - MSFT
评论
0赞
Prits
11/27/2023
感谢 Rukmini 的回答,当我拥有它时,我仍然没有该许可证,我会尝试它,如果我遇到任何问题,请将其粘贴到此处。
评论