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

孤街醉人 提交于 2020-05-17 06:01:58

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/61630690/how-to-patch-an-existing-application-using-python-azure-sdk-and-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!