我在 Azure Active Directory 中创建了应用程序注册,并添加了访问 Azure 存储的 API 权限。
我还创建了用户和应用角色分配,如下所示:
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $servicePrincipal.ObjectId -Id ([Guid]::Empty)
我在 Azure 门户中为用户授予了存储 Blob 数据贡献者角色。然后我通过这个方法获得了一个访问 token :
public void acquireToken(android.app.Activity activity,
String resource,
String clientId,
@Nullable String redirectUri,
@Nullable String loginHint,
@NonNull com.microsoft.aad.adal.AuthenticationCallback<com.microsoft.aad.adal.AuthenticationResult> callback)
clientId是有访问存储权限的应用程序。重定向 URI 是我在门户中为应用程序设置的 URI。我已经尝试了两个资源 ID。我将该令牌作为 Authentication: Bearer header 发送。
如果我传递给 acquireToken 的资源 ID 是“https://<>.blob.core.windows.net/”,它会返回错误“受众验证失败。受众不匹配”
如果资源 ID 为“https://storage.azure.com/ ”,则返回错误“指定的容器不存在”
。
我正在使用 Retrofit 执行 PUT 操作。
public static final String CONTENT_TYPE_TEXT_PLAIN_HEADER = "Content-Type: text/plain; charset=UTF-8";
public static final String X_MS_VERSION = "x-ms-version: 2017-11-09";
public static final String X_MS_DATE = "x-ms-date";
public static final String X_MS_BLOB_CONTENT = "x-ms-blob-content-disposition: attachment; filename=\"fname.ext\"";
public static final String X_MS_BLOB_TYPE = "x-ms-blob-type: BlockBlob";
public static final String X_MS_META_M1 = "x-ms-meta-m1: v1";
public static final String X_MS_META_M2 = "x-ms-meta-m2: v2";
@Headers({CONTENT_TYPE_TEXT_PLAIN_HEADER,
X_MS_VERSION,
X_MS_BLOB_CONTENT,
X_MS_BLOB_TYPE,
X_MS_META_M1, X_MS_META_M2})
@PUT("/blob1")
Call<Void> putBlob(@Header(AUTHORIZATION) String bearerToken,
@Header(X_MS_DATE) String date,
@Body String putBody);
最佳答案
It returns the error "Audience validation failed. Audience did not match" if the resource ID I pass to acquireToken is
https://<>.blob.core.windows.net/
从 https://<>.blob.core.windows.net/
中删除尾部斜杠,它现在应该符合 Azure AD 对受众的期望。
Azure Storage resource ID
An Azure AD resource ID indicates the audience for which a token that is issued can be used to provide access to an Azure resource. In the case of Azure Storage, the resource ID may be specific to a single storage account, or it may apply to any storage account. The following table describes the values that you can provide for the resource ID:
https://<account>.blob.core.windows.net
关于android - Azure BLOB 存储 REST API - 使用 ADAL 访问 token 返回 403 和 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60884154/