pythonazuremicrosoft-graph-apiazure-sdk-python

Can´t import "ApplicationsRequestBuilder" method in python using Azure SDK


Following the official documentation, to list app registrations que must use "ApplicationsRequestBuilder", but there is no info about how to import it.

My imports are:

import pyodbc
import adal
import struct
import json  
import subprocess 
import asyncio 
from azure.identity import ClientSecretCredential, DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
from msgraph import GraphServiceClient
from msgraph.generated.models.password_credential import PasswordCredential
from msgraph.generated.applications.item.add_password.add_password_post_request_body import AddPasswordPostRequestBody

I´ve tried everything, but I´m not able to import it. The error is:

File "/azp/_work/1/s/Update_Expired_Service_Principals/sp_renewal.py", line 83, in get_sp_id
query_params = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetQueryParameters(
NameError: name 'ApplicationsRequestBuilder' is not defined

My code is:

scopes = ['https://graph.microsoft.com/.default']   
conn, credential, credentials = get_token()

graph_client = GraphServiceClient(credential, scopes=scopes)
    
async def get_sp_id():
    query_params = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetQueryParameters(
        filter = "displayName eq 'test'",
        count = True,
        top = 1,
    )

request_configuration = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetRequestConfiguration(
    query_parameters = query_params,
)
request_configuration.headers.add("ConsistencyLevel", "eventual")

app_list = await graph_client.applications.get(request_configuration = request_configuration)
if app_list:
    app = app_list[0]
    sp_id = await graph_client.applications.by_application_id(app.id).get()
    print(sp_id)
    return sp_id
return None

Solution

  • To avoid this error is required to add:

    from msgraph.generated.applications.applications_request_builder import ApplicationsRequestBuilder