如何使用 Python Azure SDK 和 Graph 修补现有应用程序?

How to patch an existing application using Python Azure SDK and Graph?

我正在尝试以编程方式将 reply_url 添加到 Azure 应用程序注册,但我收到 azure.graphrbac.models.graph_error_py3.GraphErrorException: Specified HTTP method is not allowed for the request target.

当我尝试用新 reply_urls.

更新现有应用程序时失败

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

我的代码:

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient
from azure.graphrbac.models import ApplicationUpdateParameters

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._object_id = object_id
        self._application = self._graph_client.applications.get(self._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._object_id,
            ApplicationUpdateParameters(
                reply_urls=[
                    *reply_urls,
                    reply_url]
            )
        )

更新调用看起来不错,但它依赖于遗留 API(AAD 图表)和工具。强烈建议移动到MS Graph which supports almost all Azure AD Graph operations and will fully support them all in a future. Applications成为其中之一。

您可以使用 Requests-OAuthlib or Microsoft Graph Python Client Library 来实现。

无法重现您的问题,请使用相同版本的 azure-graphrbac,我在我这边测试了您的代码,它工作正常。

testclient = GraphClient(client_id = "xxxxx",client_secret = "xxxxx", tenant_id = "xxxxx", object_id = "xxxxx")
testclient.add_reply_url(reply_url = "http://localhost:8085")

登录门户:


另外,我直接测试了sdk,都可以。

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient
from azure.graphrbac.models import ApplicationUpdateParameters

_credentials = ServicePrincipalCredentials(
            client_id="xxxxx",
            secret="xxxxx",
            tenant="xxxxx",
            resource="https://graph.windows.net"
        )
_graph_client = GraphRbacManagementClient(
            credentials=_credentials,
            tenant_id="xxxxx"
        )
app = _graph_client.applications.patch(
    application_object_id = "xxxxx",
    parameters = ApplicationUpdateParameters(reply_urls = ["http://localhost:8080","http://localhost:8081"])                            
       )