我用 Typescript 编写了一个脚本,用于从 Cosmos DB 容器中获取一些项目。现在我使用 DefaultAzureCredential 进行身份验证。由于某种原因,我收到 403 错误消息,指出我没有足够的权限。这对我来说似乎很奇怪,因为我是管理员。
我现在正在本地执行脚本以进行测试。我正在使用 VS Code,并使用其扩展登录到 Azure。我可以从 VS Code 中看到我的 Azure 资源,所以我认为一切顺利。
我现在担任以下角色:
这应该足够了吧?我是不是错过了什么?
下面是我的脚本,我认为这没什么特别的。
import { CosmosClient } from "@azure/cosmos";
import { DefaultAzureCredential } from "@azure/identity";
const credential = new DefaultAzureCredential();
const client = new CosmosClient({
endpoint:
process.env.DATABASE_ENDPOINT,
aadCredentials: credential,
});
export async function getItem(
database_name: string,
container_name: string,
id: string
) {
const { resource } = await client
.database(database_name)
.container(container_name)
.item(id, id)
.read();
return resource;
}
我收到的错误摘录: ErrorResponse:请求被身份验证 {db name} 阻止:请求被阻止,因为主体 [xxxxxxxxxxx] 不具备对资源 [/] 执行操作 [Microsoft.DocumentDB/databaseAccounts/readMetadata] 所需的 RBAC 权限。了解更多:https://aka.ms/cosmos-native-rbac。
有人知道我错过了什么吗?
我相信您正在使用 Azure AD 进行身份验证,对吗?确保您注册该应用程序并为其授予正确的权限。或者更好的说法是为服务主体 = 您的应用程序提供正确的 RBAC 权限。
[编辑]
为了在 Web 浏览器中对 Azure SDK 客户端进行身份验证,我们提供了 InteractiveBrowserCredential,它可以设置为使用重定向或弹出窗口来完成身份验证流程。必须首先在 Azure 门户中为您的 Web 应用程序创建 Azure 应用程序注册。
FIY -> 在此处输入链接描述