我想使用Electron应用程序中的GitHub Api进行Basic Auth。我正在尝试从Electron应用程序向GitHub Authorizations API发送Ajax请求以创建访问令牌。当我通过cURL执行此请求时,该请求仍然有效,但我无法将其转换为Ajax请求。
当我使用cURL时它可以工作:
curl -i -u <username>:<password> -d '{"scopes": ["gist"], "note": "Wilson"}' https://api.github.com/authorizations
但是,当我发送Ajax请求时,我会返回401 Unauthorized
。我试图使用Postman以及Electron之外的请求,我得到相同的响应。我可以看到凭据是在请求中设置的。
代码:
// AJAX
$.ajax({
url: "https://" + c.username + ":" + c.password + "@api.github.com/authorizations",
type: "POST",
data: { "scopes":["gist"], "note":"Wilson" },
success: function() {
console.log('Successful response', arguments);
}, error: function(xhr) {
console.log('error', xhr);
}
});
请求标题
POST /authorizations HTTP/1.1
Host: api.github.com
Connection: keep-alive
Content-Length: 29
Accept: */*
X-DevTools-Emulate-Network-Conditions-Client-Id: 2D4D31D9-46FE-4707-8FD3-1C6EB08A37F7
Origin: file://
Authorization: Basic <username>:<password>
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) wilson/0.0.1 Chrome/47.0.2526.110 Electron/0.36.8 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Encoding: gzip, deflate
Accept-Language: en-US
将请求复制为cURL
curl 'https://api.github.com/authorizations'
-H 'Cookie: _octo=GH1.1.1866616140.1454567890; _ga=GA1.2.1909561787.1456625032; logged_in=yes; dotcom_user=applegrain'
-H 'Origin: file://' -H 'Accept-Encoding: gzip, deflate'
-H 'Accept-Language: en-US'
-H 'Authorization: Basic <username>:<password>'
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
-H 'Accept: */*' -H 'X-DevTools-Emulate-Network-Conditions-Client-Id: 2D4D31D9-46FE-4707-8FD3-1C6EB08A37F7'
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) wilson/0.0.1 Chrome/47.0.2526.110 Electron/0.36.8 Safari/537.36'
-H 'Connection: keep-alive' --data 'scopes%5B%5D=gist¬e=Wilson' --compressed
当我将请求复制为cURL并尝试运行它时,我也得到401 unauthorized
。第一个cURL命令和后续Ajax请求之间有什么区别?这可能是电子问题吗?
谢谢 - 如果您需要任何其他信息,请与我们联系。