无法使用Azure SDK在python中导入“ApplicationsRequestBuilder”方法

gj3fmq9x  于 12个月前  发布在  Python
关注(0)|答案(2)|浏览(115)

根据官方文档,要列出应用程序注册,必须使用“ApplicationsRequestBuilder”,但没有关于如何导入它的信息。
我的进口是:

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

字符串
我尝试了所有方法,但无法导入。错误是:

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


我的代码是:

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

xjreopfe

xjreopfe1#

在我的情况下,我有下面的应用程序注册与显示名称为 *'测试'**:


的数据
要获取这些应用注册的objectID,您可以使用以下示例Python代码:

import asyncio
from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient

tenant_id = "tenantId"
client_id = "appId"
client_secret = "secret"

credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

client = GraphServiceClient(credential)

async def main():
    target_app = "test"

    result = await client.applications.get()
    applications = result.value

    filtered_applications = [app for app in applications if app.display_name == target_app]

    for app in filtered_applications:
        print(app.id)

asyncio.run(main())

字符串

回复:


7uzetpgm

7uzetpgm2#

为了避免这种错误,需要添加:

from msgraph.generated.applications.applications_request_builder import ApplicationsRequestBuilder

字符串

相关问题