javascript - Azure Active Directory AADSTS90014 - 无效请求 |

标签 javascript azure-active-directory microsoft-graph-api

我正在尝试使用 OAuth 隐式流向 Azure Active Directory 进行身份验证。

我可以从 https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize 获取代码。

我的问题是,当我对 https://login.microsoftonline.com/organizations/oauth2/v2.0/token 执行 POST 时,我得到以下内容:

error: 'invalid_request', 
error_description:
    AADSTS90014: The request body must contain the following parameter: 'grant_type'.
    Trace ID: 1ac8aa85-a56c-481d-9100-baaf6d1c2200
    Correlation ID: ecaa1339-e176-49d3-90e0-080cc0cb4b8f
    Timestamp: 2018-02-26 19:27:07Z,
error_codes: [ 90014 ],
timestamp: '2018-02-26 19:27:07Z',
trace_id: '1ac8aa85-a56c-481d-9100-baaf6d1c2200',
correlation_id: 'ecaa1339-e176-49d3-90e0-080cc0cb4b8f' 

作为引用,这是我发布到 URI 的内容

// Snippet
// I should be sending queryParams to the POST request, but I keep 
// getting the error from above and then I only posted the 
// "grant_type" as a hard value into Axios
const queryParams = {
    client_id: app_id,
    client_secret: app_pass,
    scope: 'user.read',
    redirect_uri: redirect_uri,
    grant_type: 'authorization_code'
}

await axios.post(baseUrl, {
    grant_type: 'authorization_code'
}).then(res => {
    console.log(res.data)
}).catch(err => {
    if (err.response) {
        console.error(err.response.data)
        console.error(err.response.status)
        console.error(err.response.headers)
    } else if (err.request) {
        console.error(err.request)
    } else {
        console.error('ERROR', err.message);
    }
    console.log(err.config)
})

然后扩展日志,这就是 axios 在错误响应中向我报告的内容

{ 
    adapter: [Function: httpAdapter],
    transformRequest: { '0': [Function: transformRequest] },
    transformResponse: { '0': [Function: transformResponse] },
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    validateStatus: [Function: validateStatus],
    headers: { 
        Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json;charset=utf-8',
        'Access-Control-Allow-Origin': 'http://localhost:4200',
        'User-Agent': 'axios/0.18.0',
        'Content-Length': 35 },
        method: 'post',
        url: 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token',
        data: '{"grant_type":"authorization_code"}' 
    }
}

有人能给我一些关于我哪里出错或者没有发布值的见解吗?这真是让我摸不着头脑。

最佳答案

您的问题声称您正在使用隐式授权,但您包含的代码使用授权代码授权。隐式流不使用辅助 POST/token 端点。整个流程通过一次调用完成:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=[APPLICATION ID]&response_type=token&
redirect_uri=[REDIRECT URI]&scope=[SCOPE]

我建议看一下我写的演练:v2 Endpoint & Implicit Grant .

如果您实际上正在寻找授权代码授予,那么您不会在 POST 中发送完整的负载,而仅发送 grant_type。完整的有效负载应包括(换行只是为了清楚起见):

https://login.microsoftonline.com/common/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&
code=[AUTHORIZATION CODE]&
client_id=[APPLICATION ID]&
client_secret=[PASSWORD]&
scope=[SCOPE]&
redirect_uri=[REDIRECT URI]

此外,您将此数据作为 application/json 发送;它应该是application/x-www-form-urlencoded。有关如何使用 Axios 执行此操作的详细信息,请参阅 this GitHub Issue .

有关使用授权代码的演练,请参阅Microsoft v2 Endpoint Primer .

关于javascript - Azure Active Directory AADSTS90014 - 无效请求 |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996804/

相关文章:

javascript - 为什么在添加动画 css 类时不起作用

azure - 除 Azure AD 之外的身份验证方案

node.js - 通过 api graph 将图像文件上传到 sharepoint

microsoft-graph-api - Office365 图形 API : Fetch Calendar Share Permissions

javascript - 使用 Requirejs 时,是否可以让优化器自动找到所有依赖项?

javascript - 如何从视频 HTML 元素中获取帧精确搜索?

javascript - 如果需要,如何访问父作用域?

node.js - Passport-Azure-Ad 似乎异步运行

azure - 如何使用 Azure DevOps 的 MSAL.js 获取有效的 AAD v2 token

timezone - 将 microsoft graph 时区映射到时刻