Xero-Api 和 Vapor 3 无法连接以接收令牌
Xero-Api and Vapor 3 unable to connect to recive token
我有一个 vapor3 应用程序,我试图在其中连接到 Xero Api,过程相当简单。我们将用户发送到 xero 网页,在那里他们使用详细信息登录并授权我们的连接,然后它重定向回我的 vapor 3 应用程序。然后在应用程序中,我们使用重定向中提供的代码连接到 xero 服务器,然后 xero 应该发出一个访问令牌以供以后使用。过程描述here
问题是我无法连接到 xero 以获取访问令牌。我尝试了两种方法,第一种是使用 HTTPClient,代码如下:
let m = try decoder.decode(Master.self, from: masterDoc!)
let ci = m.xeroAppKey.data(using: .utf8)?.base64EncodedString() //convert from string to base encoded
let xs = m.xeroAppSec.data(using: .utf8)?.base64EncodedString() //convert from string to base encoded
let authorization = "Basic " + ci! + ":" + xs!
print("authorisation is \(authorization)")
return HTTPClient.connect(hostname: "identity.xero.com", on: req).flatMap{client in
var httpReq = HTTPRequest(method: .POST, url: "https://identity.xero.com/connect/token")
httpReq.headers.add(name: "authorization", value: authorization)
httpReq.headers.add(name: "Content-Type", value: "x-www-form-urlencoded")
httpReq.body = HTTPBody(string: "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect")
return client.send(httpReq).flatMap{resp in
print("response is \(resp) with status \(resp.status)")
return req.future().map{
return .ok
}
}
}
有了这个,我得到了以下响应:
HTTP/1.1 301 Moved Permanently
Server: AkamaiGHost
Content-Length: 0
Location: https://identity.xero.com/connect/token
Expires: Tue, 02 Jun 2020 07:59:59 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Tue, 02 Jun 2020 07:59:59 GMT
Connection: keep-alive
这表明终点已移动,但建议的位置与我尝试连接的位置相同。我在文档中找不到暗示终点已更改的任何地方。似乎 HTTPClient 也不会遵循重定向。
所以我尝试使用 URLSession 连接,而不是使用此代码:
let url = URL(string:"https://identity.xero.com/connect/token")!
let payload = "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect".data(using: .utf8)
let promise = req.eventLoop.newPromise(HTTPStatus.self)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue(authorization, forHTTPHeaderField: "authorization")
request.addValue("x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = payload
URLSession.shared.dataTask(with: request) {(data, response, error) in
if let error = error {
print(error.localizedDescription)
promise.fail(error: error) // (!)
return
}
guard let data = data else {
print("empty data")
promise.fail(error: SumUpError.invalidResponse) // (!)
return
}
guard let str = String(data: data, encoding: .utf8) else {
print("invalid data")
promise.fail(error: SumUpError.invalidResponse) // (!)
return
}
print(str)
print("response is \(String(describing: response))")
}.resume()
我在哪里收到以下错误消息。
{"error":"invalid_request"}
任何关于这里发生的事情或者我如何建立这个连接的想法都非常感谢 运行。
好的,所以最后的答案是使用在 req.client()
找到的 vapor 客户端服务
我使用了以下代码。我还犯了一个新手错误,即对客户端 ID 和客户端密码进行编码,然后将它们组合在一起,而不是先将它们组合在一起,然后再对它们进行编码。文档确实显示了这一点,但我想还不够清楚。
无论如何,这是使用 Vapor 3 连接到 Xero Api 的代码。
let toBase = "\(clientId):\(clientSecret)".data(using: .utf8)?.base64EncodedString()
let authorization = "Basic " + toBase!
let payload = "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect".data(using: .utf8)!
let request = Request( using: req)
request.http.url = URL(string:"https://identity.xero.com/connect/token")!
request.http.body = payload.convertToHTTPBody()
request.http.method = HTTPMethod.POST
request.http.headers.add(name: "authorization", value: authorization)
request.http.headers.add(name: "Content-Type", value: "application/x-www-form-urlencoded")
return try req.client().send(request).flatMap{resp in
}
希望这对尝试使用 Vapor 添加 xero 的人有所帮助。
我有一个 vapor3 应用程序,我试图在其中连接到 Xero Api,过程相当简单。我们将用户发送到 xero 网页,在那里他们使用详细信息登录并授权我们的连接,然后它重定向回我的 vapor 3 应用程序。然后在应用程序中,我们使用重定向中提供的代码连接到 xero 服务器,然后 xero 应该发出一个访问令牌以供以后使用。过程描述here
问题是我无法连接到 xero 以获取访问令牌。我尝试了两种方法,第一种是使用 HTTPClient,代码如下:
let m = try decoder.decode(Master.self, from: masterDoc!)
let ci = m.xeroAppKey.data(using: .utf8)?.base64EncodedString() //convert from string to base encoded
let xs = m.xeroAppSec.data(using: .utf8)?.base64EncodedString() //convert from string to base encoded
let authorization = "Basic " + ci! + ":" + xs!
print("authorisation is \(authorization)")
return HTTPClient.connect(hostname: "identity.xero.com", on: req).flatMap{client in
var httpReq = HTTPRequest(method: .POST, url: "https://identity.xero.com/connect/token")
httpReq.headers.add(name: "authorization", value: authorization)
httpReq.headers.add(name: "Content-Type", value: "x-www-form-urlencoded")
httpReq.body = HTTPBody(string: "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect")
return client.send(httpReq).flatMap{resp in
print("response is \(resp) with status \(resp.status)")
return req.future().map{
return .ok
}
}
}
有了这个,我得到了以下响应:
HTTP/1.1 301 Moved Permanently
Server: AkamaiGHost
Content-Length: 0
Location: https://identity.xero.com/connect/token
Expires: Tue, 02 Jun 2020 07:59:59 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Tue, 02 Jun 2020 07:59:59 GMT
Connection: keep-alive
这表明终点已移动,但建议的位置与我尝试连接的位置相同。我在文档中找不到暗示终点已更改的任何地方。似乎 HTTPClient 也不会遵循重定向。
所以我尝试使用 URLSession 连接,而不是使用此代码:
let url = URL(string:"https://identity.xero.com/connect/token")!
let payload = "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect".data(using: .utf8)
let promise = req.eventLoop.newPromise(HTTPStatus.self)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue(authorization, forHTTPHeaderField: "authorization")
request.addValue("x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = payload
URLSession.shared.dataTask(with: request) {(data, response, error) in
if let error = error {
print(error.localizedDescription)
promise.fail(error: error) // (!)
return
}
guard let data = data else {
print("empty data")
promise.fail(error: SumUpError.invalidResponse) // (!)
return
}
guard let str = String(data: data, encoding: .utf8) else {
print("invalid data")
promise.fail(error: SumUpError.invalidResponse) // (!)
return
}
print(str)
print("response is \(String(describing: response))")
}.resume()
我在哪里收到以下错误消息。
{"error":"invalid_request"}
任何关于这里发生的事情或者我如何建立这个连接的想法都非常感谢 运行。
好的,所以最后的答案是使用在 req.client()
找到的 vapor 客户端服务我使用了以下代码。我还犯了一个新手错误,即对客户端 ID 和客户端密码进行编码,然后将它们组合在一起,而不是先将它们组合在一起,然后再对它们进行编码。文档确实显示了这一点,但我想还不够清楚。 无论如何,这是使用 Vapor 3 连接到 Xero Api 的代码。
let toBase = "\(clientId):\(clientSecret)".data(using: .utf8)?.base64EncodedString()
let authorization = "Basic " + toBase!
let payload = "grant_type=authorization_code&code=\(code)&redirect_uri=http://localhost:8080/XeroAuthRedirect".data(using: .utf8)!
let request = Request( using: req)
request.http.url = URL(string:"https://identity.xero.com/connect/token")!
request.http.body = payload.convertToHTTPBody()
request.http.method = HTTPMethod.POST
request.http.headers.add(name: "authorization", value: authorization)
request.http.headers.add(name: "Content-Type", value: "application/x-www-form-urlencoded")
return try req.client().send(request).flatMap{resp in
}
希望这对尝试使用 Vapor 添加 xero 的人有所帮助。