gpt4 book ai didi

javascript - 使用 GM_xmlhttpRequest 为访问 token 交换 Quire 授权代码

转载 作者:行者123 更新时间:2023-11-30 19:12:11 27 4
gpt4 key购买 nike

这可能是个愚蠢的问题,我已尝试按照 quire-api-blog 给出的说明进行操作但我仍然无法从 Tampermonkey javascript 用户脚本获取 token 。

GM_xmlhttpRequest 的 FYI 语法在 https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest 上可用

我正在使用以下代码:

GM_xmlhttpRequest({
method: "POST",
url: "https://quire.io/oauth/token",
data: JSON.stringify({
grant_type: "authorization_code",
code: "xxx",
client_id: ":yyy",
client_secret: "zzz"
}),
onload: function(r){
console.log(r);
}
});

这会在控制台中返回以下对象:

finalUrl: "https://quire.io/oauth/token"

readyState: 4

response:

responseHeaders: "content-encoding: gzip\r\ncontent-type: text/plain; charset=utf-8\r\ndate: Sun, 13 Oct 2019 09:04:26 GMT\r\nserver: nginx/1.17.3\r\nset-cookie: DARTSESSID=7d20dcf1f0eae6ce0f69d9fe615e9ce5; Path=/; HttpOnly\r\nx-content-type-options: nosniff\r\nx-firefox-spdy: h2\r\nx-frame-options: SAMEORIGIN\r\nx-xss-protection: 1; mode=block\r\n"

responseText:

responseXML:

status: 400

statusText: "Bad Request"

知道哪里出了问题吗?

预先感谢您的友好回答。

拉斐尔

最佳答案

您需要注意请求的content-type。不同的 XHR API 使用不同的默认值。

OAUTH2 Specification for the Access Token Request将 Content-Type 描述为 application/x-www-form-urlencoded

虽然 GreaseMonkey 默认使用 JSON 发送请求,但可以通过设置 Content-Type 来更改。 header 并使用“x-www-form-urlcoded”将数据编码为字符串

GM.xmlHttpRequest({
method: "POST",
url: "https://quire.io/oauth/token",
data: "grant_type=authorization_code&code=xxx&client_id=yyy&client_secret=zzz",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},

jquery.ajax()这会自动将默认的 Content-Type 设置为 application/x-www-form-urlencoded

重要的边注:您对 $.ajax() 的使用表明在浏览器中的使用。如果那是真的,那么请不要这样做!将您的 client_secret 暴露给在浏览器内运行的应用程序将允许任何人验证您的身份并使用 grant_type: client_authentication 访问您的项目。截至目前,Quire API 要求您运行专用服务器,您必须从该服务器执行访问 token 请求,以避免暴露 client_secret。如果您在服务器端使用 jquery,那没关系。

有开放Issue#8还支持客户端 authorization_code 流而不使用 client_secret(适用于 SPA 或浏览器扩展)。

关于javascript - 使用 GM_xmlhttpRequest 为访问 token 交换 Quire 授权代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58362125/

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