首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xero-Api和Vapor 3无法连接到接收令牌

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

Stack Overflow用户
提问于 2020-06-02 16:09:48
回答 1查看 104关注 0票数 3

我有一个vapor3应用程序,在里面我试图连接到Xero Api,过程相当简单。我们将用户发送到xero网页,在那里他们使用详细信息登录并授权我们的连接,然后重定向回我的vapor 3应用程序。在应用程序中,我们使用重定向中提供的代码连接到xero服务器,然后xero应该会发出一个访问令牌,以便继续使用。该过程在here中描述

问题是我无法连接到xero来获取访问令牌。我尝试了两种方法,第一种是使用HTTPClient,代码如下:

代码语言:javascript
复制
 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
         }  
      }
    } 

这样,我得到了以下响应:

代码语言:javascript
复制
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连接,而不是使用以下代码:

代码语言:javascript
复制
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()

在那里我得到了以下错误消息。

代码语言:javascript
复制
{"error":"invalid_request"}

任何想法,这里发生了什么,或者我如何才能让这个连接启动和运行,都是非常可取的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-02 23:16:31

好的,所以最后的答案是使用req.client()中的vapor客户端服务。

我使用了以下代码。我还犯了一个新手错误,对客户id和客户机密进行编码,然后对它们进行梳理,而不是先将它们组合在一起,然后再对它们进行编码。文档确实显示了这一点,但我猜还不够清楚。不管怎样,这是用Vapor 3连接Xero Api的代码。

代码语言:javascript
复制
    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的人都有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62147572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档