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

Cypress请求生成token时提示AADSTS900144 grant_type参数缺失问题

问题根因

Azure AD的OAuth 2.0 token端点要求请求体格式必须为application/x-www-form-urlencoded,你当前的代码直接传JSON格式的body,Cypress默认会按JSON格式发送请求,导致端点无法正确解析参数,因此抛出缺少grant_type的错误。

修正后代码
Cypress.Commands.add('getToken', () => {
    cy.request({
        method: 'POST',
        url: 'https://login.microsoftonline.com/你的租户ID/oauth2/v2.0/token',
        // 新增form配置,自动将body转换为x-www-form-urlencoded格式
        form: true,
        body: {
            grant_type: 'client_credentials',
            client_secret: '你的客户端密钥',
            client_id: '你的客户端ID',
            scope: 'https://btest.com/.default'
        },
    }).then((response) => {
        const access_token = response.body.access_token
        // 可将token存入Cypress环境变量,方便后续接口调用直接使用
        Cypress.env('accessToken', access_token)
        console.log('token获取成功', access_token)
    })
})
额外注意事项
  • 不要把client_secret等敏感信息硬编码在代码里,可放到Cypress的环境变量配置文件中读取,避免密钥泄露
  • 如果你需要手动序列化请求体,也可以安装qs库后将body参数转为字符串,效果和设置form: true一致
  • 检查url中的租户ID、client_id、client_secret、scope等值是否正确,避免参数错误导致的其他400报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 13:21:00