简体   繁体   中英

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

I am trying to add a reply_url programmatically to an Azure app registration, but I receive an azure.graphrbac.models.graph_error_py3.GraphErrorException: Specified HTTP method is not allowed for the request target.

It fails when I try to update an existing application with new reply_urls .

SDK I am using is: azure-graphrbac==0.61.1

My code:

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]
            )
        )

Update call looks good however it depends on a legacy API (AAD Graph) and tools. It's strongly recommended to move to MS Graph which supports almost all Azure AD Graph operations and will fully support them all in a future . Applications being one of them.

You can use Requests-OAuthlib or Microsoft Graph Python Client Library for this.

Could not reproduce your issue, use the same version of azure-graphrbac , I test your code on my side, it works fine.

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

在此处输入图像描述

Check in the portal:

在此处输入图像描述


Also, I test the sdk directly, both work.

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"])                            
       )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM