这可能是一个愚蠢的问题,我已经尝试遵循quire-api-blog的指示,但是我仍然很难从Tampermonkey用户脚本中获得一个令牌。
GM_xmlhttpRequest的FYI语法可在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"
知道出了什么问题吗?
提前感谢您的好意答复。
拉法
发布于 2019-10-14 05:03:51
您需要在请求的content-type
上小心。不同的XHR使用不同的默认值。
访问令牌请求的OAUTH2规范描述要成为application/x-www-form-urlencoded
的内容类型。
GreaseMonkey默认使用JSON发送请求,可以更改,方法是设置内容类型头并使用“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()会自动将默认的内容类型设置为application/x-www-form-urlencoded
重要的边注:您使用$.ajax()表示在浏览器中的用法。如果那是真的,那就别这么做!将您的client_secret公开给运行在浏览器中的应用程序将允许任何人以您的身份验证身份并使用grant_type: client_authentication
访问您的项目。到目前为止,Quire API要求您运行一个专用服务器,您必须从该服务器执行访问令牌请求,以避免公开client_secret
。如果您在服务器端使用jquery,那么没关系。
还有一个开放的Issue#8可以支持客户端的authorization_code流,而不需要使用client_secret (适合从SPA或浏览器扩展)。
发布于 2019-10-13 13:59:23
同时,关于它的价值,我已经能够让它使用jQuery $.ajax()命令,请参阅https://api.jquery.com/jquery.ajax/,它提供:
$.ajax({
type: "POST",
url: "https://quire.io/oauth/token",
data: {
grant_type: "authorization_code",
code: "xxx",
client_id: ":yyy",
client_secret: "zzz"
},
success: function(r){
console.log(r);
}
});
仍然好奇地想从知识的角度了解GM_xmlhttpRequest
到底出了什么问题
https://stackoverflow.com/questions/58362125
复制相似问题