gpt4 book ai didi

jquery - Okta API 或标准 CURL/ajax 问题

转载 作者:行者123 更新时间:2023-12-01 07:43:14 28 4
gpt4 key购买 nike

我希望从 Okta API 中提取当前用户的登录名,但我通常只是一个 HTML/CSS 人员,所以我很困难。我目前陷入困境:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
jQuery.ajax({
url: "https://harmelin.okta.com/api/v1/users/me",

type: 'GET',
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});

</script>
</head>

<body>
<script type="text/javascript">
document.write(data.profile.login);
</script>
</body>

我过去曾将部分代码与其他 API 一起使用,但不得不对其进行一些修改。我知道我的问题是授权,因为我收到 403 错误。 Okta 文档将此作为请求示例:

curl -v -X GET \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: SSWS ${api_token}" \
"https://${org}.okta.com/api/v1/users/me"

如何在我的脚本中获得授权?这能为我提供 future 开发所需的内容(data.profile.login)吗?

最佳答案

您不应该在来自浏览器的请求中包含 SSWS token 。任何阅读您页面源代码的人都可以使用它来对您的组织进行修改。

如果您收到 403,这是因为用户未登录或未发送 cookie。

如果用户未登录,则应使用 the okta-signin-widget 登录, okta-auth-js ,或your login page .

在您的情况下,未发送 cookie,因为它是 CORS 请求。默认情况下,jQuery 不会在跨域请求上发送 cookie。您可以通过在请求中发送 withCredentials 来解决此问题。

用户登录后,以下请求应该起作用:

jQuery.ajax({
url: "https://harmelin.okta.com/api/v1/users/me",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
xhrFields: {
withCredentials: true
},
success: function (data) {
alert(JSON.stringify(data));
},
error: function(err){
alert(JSON.stringify(err));
}
});

关于jquery - Okta API 或标准 CURL/ajax 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43964119/

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