You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Microsoft Graph Python SDK调用devices.get()遇ValueError求助

问题描述

我基于Microsoft Graph Python SDK开发工具,从Azure AD(Entra)拉取所有设备。已确认API凭据有效、权限配置正确,代码如下:

async def get_AADdevices():
    MScredential = ClientSecretCredential(
        tenant_id=os.environ['MSGraphTenantID'],
        client_id=os.environ['MSGraphClientID'],
        client_secret=os.environ['MSGraphClientSecret']
    )
    scopes = ['https://graph.microsoft.com/.default']
    client = GraphServiceClient(credentials=MScredential, scopes=scopes)
    devices = await client.devices.get()

调用await client.devices.get()时触发如下错误:

Executed 'Functions.MachineReconciliation' (Failed, Id=d6a7b205-25fc-4dfa-ad22-3621ed0b4144, Duration=9ms)
System.Private.CoreLib: Exception while executing function: Functions.MachineReconciliation. System.Private.CoreLib: Result: Failure
Exception: ValueError: not enough values to unpack (expected 2, got 1)
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 479, in _handle__invocation_request
    call_result = await self._loop.run_in_executor(
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 752, in _run_sync_func
    return ExtensionManager.get_sync_invocation_wrapper(context,
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
    result = function(**args)
  File "C:\Users\<path>\function_app.py", line 17, in MachineReconciliation
    asyncio.run(get_AADdevices())
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\<path>\function_app.py", line 40, in get_AADdevices
    devices = await client.devices.get()
  File "C:\Users\<path>\.venv\lib\site-packages\msgraph\generated\devices\devices_request_builder.py", line 70, in get
    return await self.request_adapter.send_async(request_info, DeviceCollectionResponse, error_mapping)
  File "C:\Users\<path>\.venv\lib\site-packages\kiota_http\httpx_request_adapter.py", line 162, in send_async
    parent_span = self.start_tracing_span(request_info, "send_async")
  File "C:\Users\<path>\.venv\lib\site-packages\kiota_http\httpx_request_adapter.py", line 131, in start_tracing_span
    uri_template = ParametersNameDecodingHandler.decode_uri_encoded_string(
  File "C:\Users\<path>\.venv\lib\site-packages\kiota_http\middleware\parameters_name_decoding_handler.py", line 92, in decode_uri_encoded_string
    name, value = name_value.split('=')

预期返回设备列表,求解决思路。

解决思路

从报错栈分析,问题根源是kiota_http库的参数解码逻辑异常,尝试用=分割URI参数时只得到单个值,无法完成解构。可按以下步骤排查修复:

  • 检查环境变量合法性
    确认MSGraphTenantID、MSGraphClientID、MSGraphClientSecret等环境变量未包含=、&这类特殊字符,避免污染请求URI的参数格式。可以临时打印生成的请求URI(通过client.devices.get_build_request()获取),检查是否存在无值的异常参数。

  • 更新/调整依赖版本
    该问题多为kiota_http或msgraph-sdk-python的版本bug,优先尝试升级到最新稳定版:

    pip install --upgrade msgraph-sdk-python kiota-http
    

    若最新版仍有问题,可降级到已知稳定的版本,比如指定kiota-httpx==0.7.0、msgraph-sdk-python==1.0.0。

  • 禁用参数解码中间件
    临时绕过问题中间件验证请求是否正常:

    from kiota_http.middleware.parameters_name_decoding_handler import ParametersNameDecodingHandler
    from kiota_http.httpx_request_adapter import HttpxRequestAdapter
    
    async def get_AADdevices():
        MScredential = ClientSecretCredential(
            tenant_id=os.environ['MSGraphTenantID'],
            client_id=os.environ['MSGraphClientID'],
            client_secret=os.environ['MSGraphClientSecret']
        )
        scopes = ['https://graph.microsoft.com/.default']
        # 创建请求适配器并移除问题中间件
        adapter = HttpxRequestAdapter(MScredential)
        adapter.middleware.remove(ParametersNameDecodingHandler)
        client = GraphServiceClient(request_adapter=adapter, scopes=scopes)
        devices = await client.devices.get()
    
  • 切换同步请求作为临时方案
    若异步请求的问题无法快速解决,可尝试同步版本的调用方式:

    def get_AADdevices():
        MScredential = ClientSecretCredential(
            tenant_id=os.environ['MSGraphTenantID'],
            client_id=os.environ['MSGraphClientID'],
            client_secret=os.environ['MSGraphClientSecret']
        )
        scopes = ['https://graph.microsoft.com/.default']
        client = GraphServiceClient(credentials=MScredential, scopes=scopes)
        devices = client.devices.get(request_configuration=None)
    

内容的提问来源于stack exchange,提问作者Daryl Clark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 03:25:04