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

MSGraph SDK Python代码在部分Windows设备运行报错ValueError求助

解决MSGraph SDK Python中ValueError: not enough values to unpack (expected 2, got 1)错误

问题情况

按照微软MSGraph Python SDK教程编写的代码,在Windows 11笔记本运行正常,但在Windows 10台式机及其他Windows 11设备上触发错误,根源是kiota_http库的ParametersNameDecodingHandler.decode_uri_encoded_string方法处理URL参数时,尝试用=分割参数时遇到无值的参数,导致解包失败。

涉及代码片段

async def get_user(self):
        # Only request specific properties using $select
        query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(
            select=['displayName', 'mail', 'userPrincipalName']
        )

        request_config = UserItemRequestBuilder.UserItemRequestBuilderGetRequestConfiguration(
            query_parameters=query_params
        )
        
        user =  await self.user_client.me.get(request_configuration=request_config)

完整错误堆栈

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\jfarr\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\jfarr\OneDrive\Documents\GitHub\SwordPhish\python_server\src\main.py", line 96, in <module>
    asyncio.run(main())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\jfarr\OneDrive\Documents\GitHub\SwordPhish\python_server\src\main.py", line 18, in main
    await greet_user(graph)
  File "C:\Users\jfarr\OneDrive\Documents\GitHub\SwordPhish\python_server\src\main.py", line 51, in greet_user
    user = await graph.get_user()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jfarr\OneDrive\Documents\GitHub\SwordPhish\python_server\src\api_outlook.py", line 49, in get_user
    user =  await self.user_client.me.get(request_configuration=request_config)  
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File "C:\Users\jfarr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\msgraph\generated\users\item\user_item_request_builder.py", line 155, in get
    return await self.request_adapter.send_async(request_info, User, error_mapping)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jfarr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\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\jfarr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\kiota_http\httpx_request_adapter.py", line 131, in start_tracing_span
    uri_template = ParametersNameDecodingHandler.decode_uri_encoded_string(      
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      
  File "C:\Users\jfarr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\kiota_http\middleware\parameters_name_decoding_handler.py", line 92, in decode_uri_encoded_string
    name, value = name_value.split('=')
    ^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

解决方案

1. 禁用参数名称解码中间件

该错误是kiota_http库的中间件bug,临时移除该中间件可绕过问题:

from kiota_http.middleware.parameters_name_decoding_handler import ParametersNameDecodingHandler

# 在构建请求适配器后移除目标中间件
adapter = HttpRequestAdapter(...)
target_middleware = next(m for m in adapter.middleware if isinstance(m, ParametersNameDecodingHandler))
adapter.middleware.remove(target_middleware)

2. 升级kiota_http到最新版本

该bug已在kiota_http后续版本中修复,执行升级命令:

pip install --upgrade kiota_http

3. 直接构造查询字符串

如果前两种方法无效,可绕过SDK参数构造逻辑,直接传入原始查询参数:

user = await self.user_client.me.get(request_configuration=UserItemRequestBuilder.UserItemRequestBuilderGetRequestConfiguration(
    query_parameters={
        '$select': 'displayName,mail,userPrincipalName'
    }
))

已尝试的无效操作

  • 卸载重装所有依赖包
  • 更换不同版本的依赖组合

内容的提问来源于stack exchange,提问作者Jonathan Farrar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 21:32:01