你需要小心content-type
您的要求。不同的 XHR API 使用不同的默认值。
The 访问令牌请求的 OAUTH2 规范 https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3将 Content-Type 描述为application/x-www-form-urlencoded
.
虽然 GreaseMonkey 默认使用 JSON 发送请求,但可以通过设置来更改内容类型 https://wiki.greasespot.net/GM.xmlHttpRequest#POST_request标头并使用“x-www-form-urlencoded”将数据编码为字符串
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() https://api.jquery.com/JQuery.ajax/这会自动将默认内容类型设置为 application/x-www-form-urlencoded
重要附注:您对 $.ajax() 的使用表明了在浏览器中的使用情况。如果这是真的那就不要这样做!将您的 client_secret 暴露给浏览器内运行的应用程序将允许任何人以您的查询身份进行身份验证并使用grant_type: client_authentication
。截至目前,Quire API 要求您运行专用服务器,您必须从中执行访问令牌请求,以避免暴露client_secret
。如果您在服务器端使用 jquery,那么就可以了。
有开放的Issue#8 https://github.com/quire-api/quire-api/issues/8还支持客户端授权代码流,而不使用 client_secret(适合来自 SPA 或浏览器扩展)。