Xero-Api和Vapor 3无法连接到接收令牌

时间:2020-06-02 08:09:47

标签: swift vapor xero-api

我有一个vapor3应用程序,试图在其中连接到Xero Api,此过程非常简单。我们将用户发送到xero网页,他们在其中登录并提供详细信息并授权我们的连接,然后重定向回我的vapor 3应用程序。然后在应用程序中,使用重定向中提供的代码连接到xero服务器,然后应向xero颁发访问令牌以供日后使用。 here

中描述了该过程

问题是我无法连接到xero以获得访问令牌。我尝试了2种方法,第一种是使用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"}

任何想法都在这里发生了什么,或者我如何建立并运行此连接,对此深表感谢。

1 个答案:

答案 0 :(得分:1)

好吧,最后的答案是使用req.client()中找到的蒸气客户端服务

我使用了以下代码。我还犯了一个新手错误,即对客户端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的人有所帮助。