【问题标题】:How to perform a post with Alamofire (swift) to get a security token as return value如何使用 Alamofire (swift) 执行发布以获取安全令牌作为返回值
【发布时间】:2019-10-08 08:17:45
【问题描述】:

我想使用 Swift Alamofire 通过 cURL 请求令牌。但是,到目前为止,我无法弄清楚如何以正确的顺序传递正确的参数以使其工作。

艾玛迪斯给出的描述如下:

 let headers = [
    "Content-Type": "application/x-www-form-urlencoded"
]

let params = [
    "grant_type": "client_credentials"
]


Alamofire.request("https://test.api.amadeus.com/v1/security/oauth2/token", method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).authenticate(user: "API", password: "API").responseJSON { response in debugPrint(response)

    let result = response.result.value
    print(result)
}

预期的结果是获取 JSON 文件作为返回,包括发出 API 请求的令牌。非常感谢任何帮助。谢谢

【问题讨论】:

标签: swift post alamofire amadeus


【解决方案1】:

既然您有一个cURL 示例,我们就可以使用 Alamofire 的有用调试工具。

让我们破解你当前的代码:

let headers = ["Content-Type": "application/x-www-form-urlencoded"]

let params = ["grant_type": "client_credentials"]

let request = Alamofire.request("https://test.api.amadeus.com/v1/security/oauth2/token",
                                method: .post,
                                parameters: params,
                                encoding: JSONEncoding.default,
                                headers: headers).authenticate(user: "API", password: "API")
print(request.debugDescription)
request.responseJSON { response in
    debugPrint(response)
    let result = response.result.value
    print(result)
}

输出:

$>curl -v \
-X POST \
-u API:API \
-H "Accept-Language: fr-US;q=1.0, en;q=0.9, fr-FR;q=0.8" \
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \
-H "User-Agent: iOSTest/1.0 (nt.iOSTest; build:1; iOS 12.2.0) Alamofire/4.8.1" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "{\"grant_type\":\"client_credentials\"}" \
"https://test.api.amadeus.com/v1/security/oauth2/token"

让我们一步一步来: 我们应该忘记 Accept-Encoding、User-Agent 和 Accept-Language 标头。我稍后会跳过它们。

我们看到-d(httpBody 中的数据)是错误的。

让我们将其更改为:encoding: JSONEncoding.defaultencoding: URLEncoding(destination: .httpBody)。另外,这是有道理的,因为在内容类型中我们说它是 url 编码的。

然后我们得到:

$>-d "grant_type=client_credentials"

看起来好多了。

我们看到-u API:API \ 对应于.authenticate(user: "API", password: "API")。如果服务器不管理这样的身份验证,我们可能希望将其删除,并将其放入参数中。 所以现在,让我们更改参数:

let params = ["grant_type": "client_credentials",
              "client_id" : "APIKey",
              "client_secret" : "APISecret"]

我们得到:

$>-d "client_id=APIKey&client_secret=APISecret&grant_type=client_credentials" \

那么它应该可以工作了。

顺序不一样,但服务器不应该关心它。它应该是键/访问而不是索引/访问。

最后:

let headers = ["Content-Type": "application/x-www-form-urlencoded"]

let params = ["grant_type": "client_credentials",
              "client_id" : "APIKey",
              "client_secret" : "APISecret"]

let request = Alamofire.request("https://test.api.amadeus.com/v1/security/oauth2/token",
                                method: .post,
                                parameters: params,
                                encoding: URLEncoding(destination: .httpBody),
                                headers: headers)
print(request.debugDescription)
request.responseJSON { response in
    debugPrint(response)
    let result = response.result.value
    print(result)
}

输出(带有跳过的标题):

$ curl -v \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=APIKey&client_secret=APISecret&grant_type=client_credentials" \
"https://test.api.amadeus.com/v1/security/oauth2/token"

【讨论】:

  • 首先:您的解决方案正是我想要的。感谢那。我编辑了您的答案并添加了 API 调用。不幸的是,它返回未知。有什么建议为什么会出现这种情况?
  • BTW 安全令牌存储来自 .post 方法的值
  • 不要试图用另一个问题来编辑我的答案。而是创建一个新问题。
  • 对不起。创建了一个新问题
【解决方案2】:

必须作为正文传递的字符串是:

let params = [
    "grant_type=client_credentials&client_id=CLIENTIDHERE&client_secret=CLIENTSECRETHERE"
]

我对 Swift 了解不多,但我想你可以做到:

let params = [
    "grant_type": "client_credentials"
    "client_id" : "Your API Key"
    "client_secret" : "Your API Secret"
]

【讨论】:

    【解决方案3】:

    文档说参数应该包括grant_type:client_credentials&client_id={client_id}&client_secret={client_secret}。您的参数缺少客户端 ID 和客户端密码。确保通过执行以下操作来包含这些内容:

    let params = [
        "grant_type": "client_credentials&client_id=CLIENTIDHERE&client_secret=CLIENTSECRETHERE"
    ]
    

    【讨论】:

    • 谢谢。不幸的是,amadeus 服务器响应错误 = "invalid_request"; "error_description" = "body 中只允许使用 client_id、client_secret 和 grant_type 参数"; title = "无效参数";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多