Azure Storage Blob PHP客户端:createBlobServiceWithTokenCredential调用报错求助
Azure Storage Blob PHP客户端库授权头格式错误问题解决
问题重现
使用BlobRestProxy::createBlobServiceWithTokenCredential方法创建Blob服务客户端时,执行createBlockBlob操作抛出错误:
Authentication information is not given in the correct format. Check the value of Authorization header.
已确认令牌可通过REST API正常调用接口,代码实现如下:
$connectionString = "DefaultEndpointsProtocol=https;BlobEndpoint=https://..."; $access_token = get_token_access($application_id, $application_secret, 'https://...'); $blobClient = BlobRestProxy::createBlobServiceWithTokenCredential($accessToken,$connectionString); $blobClient->createBlockBlob($containerName, $fileName, $content);
解决步骤
1. 修正变量大小写问题
PHP变量区分大小写,代码中定义的是$access_token,但传递给方法的是$accessToken,这会导致传递未定义的变量,先统一变量名:
$accessToken = get_token_access($application_id, $application_secret, 'https://...');
2. 调整连接字符串格式
使用令牌凭证时,连接字符串不能包含任何认证相关参数(如AccountKey、SharedAccessSignature),仅保留端点基础信息:
$connectionString = "DefaultEndpointsProtocol=https;BlobEndpoint=https://your-storage-account.blob.core.windows.net/";
3. 正确封装令牌凭证(针对部分库版本)
部分Azure Storage PHP客户端库版本要求传入TokenCredential对象而非原始令牌字符串,需先实例化凭证对象:
use Azure\Storage\Common\TokenCredential; $accessToken = get_token_access($application_id, $application_secret, 'https://storage.azure.com/'); $tokenCredential = new TokenCredential($accessToken); $blobClient = BlobRestProxy::createBlobServiceWithTokenCredential($tokenCredential, $connectionString);
4. 验证令牌受众(aud)
确保获取令牌时指定的resource/scope为https://storage.azure.com/或目标存储账户的Blob端点,令牌的aud字段需与该值匹配,否则会导致授权格式错误。
内容的提问来源于stack exchange,提问作者Faith Baghan
相关产品推荐
相关产品推荐

