You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:55:20