Azure 通过 javascript 获取访问令牌
Azure get access token via javascript
我正在尝试从 Azure 获取访问令牌。我正在学习 this 教程,但问题是那个人在使用邮递员。它也适用于邮递员,但它在 javascript 中失败了,我不明白为什么。
function getAccessToken() {
fetch(`${loginUrl}${tenantId}/oauth2/token`, {
method: "POST",
body: JSON.stringify({
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
resource: resource,
})
})
.then((response) => {
console.log(JSON.stringify(response));
})
.catch((err) => {
console.log(JSON.stringify(err));
});
}
凭据良好,即 clientId、secret、tenantid 等。
我也在 PowerShell 中尝试过并且成功了:
Invoke-RestMethod `
-Uri "$loginUrl$tenantId/oauth2/token" `
-Method Post `
-Body @{"grant_type"="client_credentials"; "resource" = $resource; "client_id" = $clientId; "client_secret" = $clientSecret }
但是在 js 上我得到以下错误:
Access to fetch at 'https://login.microsoftonline.com/myTenantId/oauth2/token' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我 运行 这个脚本在 HTML 文件中,目前用于测试目的。
我正在尝试从 Azure 获取访问令牌。我正在学习 this 教程,但问题是那个人在使用邮递员。它也适用于邮递员,但它在 javascript 中失败了,我不明白为什么。
function getAccessToken() {
fetch(`${loginUrl}${tenantId}/oauth2/token`, {
method: "POST",
body: JSON.stringify({
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
resource: resource,
})
})
.then((response) => {
console.log(JSON.stringify(response));
})
.catch((err) => {
console.log(JSON.stringify(err));
});
}
凭据良好,即 clientId、secret、tenantid 等。 我也在 PowerShell 中尝试过并且成功了:
Invoke-RestMethod `
-Uri "$loginUrl$tenantId/oauth2/token" `
-Method Post `
-Body @{"grant_type"="client_credentials"; "resource" = $resource; "client_id" = $clientId; "client_secret" = $clientSecret }
但是在 js 上我得到以下错误:
Access to fetch at 'https://login.microsoftonline.com/myTenantId/oauth2/token' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我 运行 这个脚本在 HTML 文件中,目前用于测试目的。