具有GM_xmlhttpRequest的访问令牌的Exchange Quire授权代码

问题描述 投票:0回答:2

这可能是一个愚蠢的问题,我尝试按照https://quire.io/blog/p/Create-Your-Quire-App-with-Quire-API.htm上的说明进行操作,但仍然无法从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);
    }
});

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

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"

任何想法出了什么问题?

先谢谢您的好回答。

拉斐尔

javascript userscripts quire-api
2个回答
0
投票

同时,对于它的价值,我已经能够使其与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出了什么问题


0
投票

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

OAUTH2 Specification for the Access Token Request将内容类型描述为application/x-www-form-urlencoded

虽然GreaseMonkey默认情况下使用JSON发送请求,可以通过设置Content-Type标头并使用'x-www-form-urlcoded'将数据编码为String来更改此请求

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

© www.soinside.com 2019 - 2024. All rights reserved.