gpt4 book ai didi

azure - 无法在 Outlook 加载项应用程序中获取 sso token

转载 作者:行者123 更新时间:2023-12-03 06:23:22 25 4
gpt4 key购买 nike

我将从后端获取 sso token ,如所述 here

在我的前面,我获得了传递给后端的访问 token

OfficeRuntime.auth
.getAccessToken({ allowConsentPrompt: true, allowSignInPrompt: true, forMSGraphAccess: true })
.then((token) => {
console.log(token);
axios
.get(process.env.REACT_APP_FUNC_ENDPOINT + URLS.GET_GRAPH_DATA, {
headers: { Authorization: "Bearer " + token },
})
.then((data) => {
console.log(data);
event.completed();
});
});

在后端,我用 sso token 交换访问 token

export async function getAccessToken(authorization: string): Promise<any> {
if (!authorization) {
let error = new Error("No Authorization header was found.");
return Promise.reject(error);
} else {
const scopeName: string = process.env.SCOPE || "User.Read";
const [, /* schema */ assertion] = authorization.split(" ");

const tokenScopes = (jwt.decode(assertion) as jwt.JwtPayload).scp.split(
" "
);
const accessAsUserScope = tokenScopes.find(
(scope) => scope === "access_as_user"
);
if (!accessAsUserScope) {
throw new Error("Missing access_as_user");
}

const formParams = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: assertion,
requested_token_use: "on_behalf_of",
scope: [scopeName].join(" "),
};

const stsDomain: string = "https://login.microsoftonline.com";
const tenant: string = "common";
const tokenURLSegment: string = "oauth2/v2.0/token";
const encodedForm = form(formParams);
let tokenResponse;
try {
tokenResponse = await axios.post(
`${stsDomain}/${tenant}/${tokenURLSegment}`,
encodedForm,
{
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
} catch (error) {
console.log(error);
}

return tokenResponse.data;
}
}

但我收到错误

'AADSTS65001: The user or administrator has not consented to use theapplication with ID '64f01276-0XXX' named 'ssoaddins'. Send aninteractive authorization request for this user and resource.\r\nTraceID: 9a6a62f0-e698-48f3-ac77-ca18c0bbbd00\r\nCorrelation ID:58843565-b171-4165-ab24-face78a43dfb\r\nTimestamp: 2023-03-1501:23:42Z'

在前端,我指定了 allowConsentPrompt: true 但用户没有获得同意屏幕。什么情况下会发生这种情况?

在Azure中,我定义了委托(delegate)权限,因此不需要管理员批准。

enter image description here

我不知道为什么会收到此错误?

在调整我的代码后进行更新,我得到一个看起来不是有效 token 的图形 token :

enter image description here

最佳答案

我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:

我注册了一个 Azure AD 应用程序并添加了 API 权限,如下所示:

enter image description here

现在我设置了应用程序 ID URI 并公开了一个名为 access_as_user 的 API,如下所示:

enter image description here

当我尝试通过 Postman 使用带有以下参数的代表流获取访问 token 时,我得到了相同的错误,正如您所喜欢的:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

client_id: <appID>
client_secret: <secret>
scope: User.Read access_as_user
grant_type: urn:ietf:params:oauth:grant-type:jwt-bearer
assertion: assertion
requested_token_use: on_behalf_of

回应: enter image description here

解决该错误,请尝试在应用程序的 API 权限中添加 access_as_user,如下所示:

您可以在我的 API 中找到该权限,如下所示:

enter image description here

现在,在应用程序的 API 权限中添加 access_as_user,如下所示:

enter image description here

确保为上述添加的权限授予管理员同意,如下所示:

enter image description here

如果您生成具有 access_as_user 范围的断言,则仅传递与图相关的权限,以通过代表流获取访问 token 。

当我仅在范围内传递User.Read权限时,我通过代表流程成功获取了访问 token ,如下所示:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

client_id: <appID>
client_secret: <secret>
scope: User.Read
grant_type: urn:ietf:params:oauth:grant-type:jwt-bearer
assertion: assertion
requested_token_use: on_behalf_of

回应:

enter image description here

就您的情况而言,请确保向所有添加的权限授予管理员同意,并在范围内传递图形权限,例如User.Read,而无需加入access_as_user,同时通过代表流程获取 token 。

关于azure - 无法在 Outlook 加载项应用程序中获取 sso token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75739772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com