android - Azure BLOB 存储 REST API - 使用 ADAL 访问 token 返回 403 和 404 错误

标签 android azure azure-active-directory azure-blob-storage adal

我在 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 对受众的期望。

来自https://learn.microsoft.com/en-us/azure/storage/common/storage-auth-aad-app#azure-storage-resource-id -

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/

相关文章:

java - 添加 ImageView 时应用程序崩溃

java - onError java.lang.NullPointerException : Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference 错误

Azure 应用服务编辑器(摩纳哥)停止工作/无法访问 http 错误 500.1001

azure - 是否需要通过 https ://apps. dev.microsoft.com 注册新应用程序?

java - 带有 Oauth 2 的 Azure AD 和 Spring Security

android - 原文件 'AndroidManifest.xml'已被删除或不可访问

android - Chromecast 自定义选择器对话框

javascript - 对子元素的 Azure CosmosDB 查询

azure - 找不到具有指定模式 : d:\a\r1\a\**\*. zip 的包<br/>检查任务中提到的包是否作为工件发布

azure - Azure 存储+云服务+SQL Azure 解决方案的最佳定价