如何设置正确的权限以使用 Python Azure SDK for Graph?

How to set correct privileges to use Python Azure SDK for Graph?

我正在尝试以编程方式将 reply_url 添加到 Azure 应用程序注册,但我收到 GraphErrorException: Insufficient privileges to complete the operation

问题是我不明白我的应用程序注册需要哪些权限。

基本上我使用应用程序注册的凭据来更改它自己的 reply_urls。

权限集为User.ReadApplication.ReadWrite.OwnedBy。两者都被授予。

我错过了哪一个?我怎样才能知道?

这是我正在使用的SDK:azure-graphrbac==0.61.1

我的代码如下所示:

class GraphClient:
    def __init__(self, client_id, client_secret, tenant_id, object_id):
        self._credentials = ServicePrincipalCredentials(
            client_id=client_id,
            secret=client_secret,
            tenant=tenant_id,
            resource="https://graph.windows.net"
        )
        self._graph_client = GraphRbacManagementClient(
            credentials=self._credentials,
            tenant_id=tenant_id
        )
        self._application = self._graph_client.applications.get(object_id)

    def get_reply_urls(self) -> List[str]:
        return self._application.reply_urls

    def add_reply_url(self, reply_url) -> None:
        reply_urls: list = self.get_reply_urls()
        self._graph_client.applications.patch(
            self._application.app_id,
            ApplicationUpdateParameters(
                reply_urls=[
                    *reply_urls,
                    reply_url]
            )
        )

编辑:添加权限截图

如果使用microsoft graph,资源应该是:https://graph.microsoft.com

如果使用 azure ad graph,资源应该是:https://graph.windows.net

根据您的代码,资源是 https://graph.windows.net,因此它在后端请求 azure ad graph api。 所以我们需要添加azure ad graph的权限而不是microsoft graph。

您提供的屏幕截图显示您添加了 microsoft graph 的权限 Application.ReadWrite.OwnedBy 而不是 azure ad graph。所以请删除它并添加属于 azure ad graph 的相同权限。

然后不要忘记授予管理员许可。

希望对你有帮助~