使用 GM_xmlhttpRequest 交换访问令牌的 Quire 授权代码
Exchange Quire authorization code for access token with GM_xmlhttpRequest
这可能是一个愚蠢的问题,我已尝试按照 quire-api-blog 给出的说明进行操作,但我仍然无法从 Tampermonkey javascript 用户脚本获取令牌。
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);
}
});
这个returns在控制台下面的对象:
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"
知道哪里出了问题吗?
提前感谢您的热心回答。
拉斐尔
与此同时,就其价值而言,我已经能够使它与 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
出了什么问题
您需要注意您的请求的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 暴露给浏览器内的应用程序 运行ning 将允许任何人验证您的身份并使用 grant_type: client_authentication
访问您的项目。截至目前,Quire API 要求您 运行 一个专用服务器,您必须从该服务器执行访问令牌请求,以避免暴露 client_secret
。如果您在服务器端使用 jquery,那没关系。
有开放 Issue#8 也支持客户端 authorization_code 流程而不使用 client_secret(适用于 SPA 或浏览器扩展)。
这可能是一个愚蠢的问题,我已尝试按照 quire-api-blog 给出的说明进行操作,但我仍然无法从 Tampermonkey javascript 用户脚本获取令牌。
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);
}
});
这个returns在控制台下面的对象:
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"
知道哪里出了问题吗?
提前感谢您的热心回答。
拉斐尔
与此同时,就其价值而言,我已经能够使它与 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
您需要注意您的请求的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 暴露给浏览器内的应用程序 运行ning 将允许任何人验证您的身份并使用 grant_type: client_authentication
访问您的项目。截至目前,Quire API 要求您 运行 一个专用服务器,您必须从该服务器执行访问令牌请求,以避免暴露 client_secret
。如果您在服务器端使用 jquery,那没关系。
有开放 Issue#8 也支持客户端 authorization_code 流程而不使用 client_secret(适用于 SPA 或浏览器扩展)。