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

调用Microsoft Defender for Endpoint API遇AADSTS900144错误求助

解决Microsoft Defender for Endpoint API认证提示缺少grant_type的问题

问题原因

你设置了请求头Content-Type: application/x-www-form-urlencoded,但却将请求体用json.dumps()转为JSON字符串后传给requests.post()的data参数。此时服务器会按表单格式解析请求体,但JSON字符串无法被正确识别为表单参数,因此返回"缺少grant_type"的错误。

修复方案

直接将参数字典传给data参数,requests库会自动将其编码为符合application/x-www-form-urlencoded格式的请求体,无需手动转换为JSON。

修改后的authenticate_mde函数代码:

def authenticate_mde():
    headers = {
        'content-type': 'application/x-www-form-urlencoded'
    }
    body = {
        'resource': MDE_URI,
        'client_id': MDE_CLIENT_ID,
        'client_secret': MDE_CLIENT_SECRET,
        'grant_type': 'client_credentials'
    }
    # 移除json.dumps,直接传入参数字典
    response = requests.post(AUTHORITY + TENANT_ID + '/oauth2/token', data=body, headers=headers)
    
    if (response.status_code < 200 or response.status_code > 299):
        raise RESTError(response.status_code, response.json())

    return response.json()['access_token']

额外说明

如果使用requests.post()的json参数传递数据,库会自动设置Content-Type: application/json,但Microsoft的认证接口要求用表单格式提交参数,因此必须用data传递字典,同时保持Content-Type为application/x-www-form-urlencoded。

内容的提问来源于stack exchange,提问作者Matt M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:45:31