簡體   English   中英

使用 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);
    }
});

這將在控制台中返回以下 object:

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.ZD223E1439188E478349D52476506C22E ,它給出了。

$.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 規范將 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 暴露給在瀏覽器中運行的應用程序將允許任何人作為您的 quire 身份進行身份驗證,並使用grant_type: client_authentication訪問您的項目。 截至目前,Quire API 要求您運行專用服務器,您必須從該服務器執行訪問令牌請求,以避免暴露client_secret 如果您在服務器端使用 jquery,那沒關系。

有一個開放的問題#8也支持客戶端授權代碼流,而不使用 client_secret(適用於 SPA 或瀏覽器擴展)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM