使用PHP上传文件至OneDrive遇‘Tenant does not have a SPO license’错误求助
问题解决:Tenant does not have a SPO license 错误
错误原因
这个错误说明你的Azure AD租户没有SharePoint Online (SPO) 或包含OneDrive for Business的商业许可证,当前使用的客户端凭证授权流要求租户具备此类许可证才能访问用户的OneDrive资源。
解决方案
1. 检查并配置租户及用户许可证
- 登录Azure门户,进入Azure Active Directory > 许可证 > 所有产品,确认租户已订阅包含OneDrive for Business的许可证(如Microsoft 365商业基础版/标准版、SharePoint Online计划等)。
- 为目标用户分配许可证:进入Azure Active Directory > 用户 > 选择目标用户 > 许可证和应用程序,勾选对应的OneDrive/SharePoint相关许可证并保存。
2. 验证应用权限配置
- 在Azure AD应用注册中,添加应用程序权限(而非委托权限):
Files.ReadWrite.All或Sites.ReadWrite.All,并要求管理员完成权限同意。 - 注意:应用程序权限需要全局管理员或SharePoint管理员同意后才能生效。
3. 代码优化(推荐使用Microsoft Graph SDK)
你已引入Graph SDK但未使用,直接用Guzzle易出错,改用SDK简化操作:
use Microsoft\Graph\GraphServiceClient; use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; $tenantId = '你的租户ID'; $clientId = '你的客户端ID'; $clientSecret = '你的客户端密钥'; $userId = '目标用户ID'; // 初始化凭证上下文 $credentialContext = new ClientCredentialContext( "https://login.microsoftonline.com/$tenantId", $clientId, $clientSecret ); // 创建Graph客户端 $graphClient = new GraphServiceClient($credentialContext); $filePath = '/var/www/html/folder/info.txt'; $fileContent = file_get_contents($filePath); // 上传文件到用户OneDrive try { $graphClient->users()->byUserId($userId)->drive()->root()->children()->byDriveItemName('file.txt')->content()->put($fileContent); echo '文件上传成功!'; } catch (\Exception $e) { echo '上传失败:' . $e->getMessage(); }
确保通过Composer安装SDK依赖:
composer require microsoft/microsoft-graph
4. 特殊场景排查
- 若使用个人OneDrive(非商业版):客户端凭证流不支持,需改用授权码流(Authorization Code Flow),因为个人OneDrive仅支持委托权限,需用户手动授权。
- 确认目标用户已激活OneDrive:用户首次登录OneDrive后才会创建个人驱动器,未激活会导致访问失败。
内容的提问来源于stack exchange,提问作者Prits
相关产品推荐
相关产品推荐

