微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 Active Directory Token curl 命令转换为节点获取

如何解决如何将 Active Directory Token curl 命令转换为节点获取

当我将自己的租户 ID、客户端 ID 和客户端密钥放入时成功运行的 CURL 命令:

# Replace {tenant} with your tenant!
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=535fb089-9ff3-47b6-9bfb-4f1264799865&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=qWgdYAmab0YSkuL1qKv5bPX&grant_type=client_credentials' 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token'

我尝试使用 node-fetch 出错:

const fetch = require('node-fetch');
let  tenantId='<my tenant id>';

    let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,{
        method: 'post',headers: {'Content-Type': 'application/json'},body: JSON.stringify({
            client_id: '<my client id>',scope: 'https://graph.microsoft.com',client_secret: '<my client secret>',grant_type: 'client_credentials',})
      }).then(function(response) {
        return response.json()
      }).then(json => {
          console.log(json)
      })

我收到的错误

  error: 'invalid_request',error_description: "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\n" +
    'Trace ID: <trace id>\r\n' +
    'Correlation ID: <correlation id>\r\n' +
    'Timestamp: 2021-05-12 22:27:30Z',error_codes: [ 900144 ],timestamp: '2021-05-12 22:27:30Z',trace_id: '<trace id>',correlation_id: '<correlation id>',error_uri: 'https://login.microsoftonline.com/error?code=900144'

我的 node-fetch POST 请求中的正文有什么问题?

顺便说一下,我已经尝试过 Axios 并且也得到了相同的结果。

解决方法

AD 正在等待表单请求,试试这个

const fetch = require('node-fetch');
let  tenantId='<my tenant id>';

    let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,{
        method: 'post',headers: {'Content-Type': 'application/x-www-form-urlencoded'},body: 'client_id=<my client id>&scope=https://graph.microsoft.com&client_secret=<my client secret>&grant_type=client_credentials'
        }
      }).then(function(response) {
        return response.json()
      }).then(json => {
          console.log(json)
      })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。