如何使用Alamofire(swift)执行post以获取安全令牌作为返回值

如何使用Alamofire(swift)执行post以获取安全令牌作为返回值,swift,post,alamofire,amadeus,Swift,Post,Alamofire,Amadeus,我想使用Swift Alamofire通过cURL请求令牌。然而,到目前为止,我还不知道如何以正确的顺序传递正确的参数以使其工作 Amadeus给出的说明如下: 预期的结果是获得JSON文件作为回报,包括发出API请求的令牌。非常感谢您的帮助。谢谢文档说参数应该包括grant\u type:client\u credentials&client\u id={client\u id}和client\u secret={client\u secret}。您的参数缺少客户端ID和客户端密码。确保通过

我想使用Swift Alamofire通过cURL请求令牌。然而,到目前为止,我还不知道如何以正确的顺序传递正确的参数以使其工作

Amadeus给出的说明如下:


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

文档说参数应该包括
grant\u type:client\u credentials&client\u id={client\u id}和client\u secret={client\u secret}
。您的参数缺少客户端ID和客户端密码。确保通过执行以下操作将这些内容包括在内:

让参数=[
“授权类型”:“客户端凭据&客户端id=clientdhere&客户端机密=CLIENTSECRETHERE”
]

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

让参数=[
“授予\类型=客户端\凭据&客户端\ id=客户端凭据&客户端\机密=客户端加密”
]
我对斯威夫特了解不多,但我想你可以:

让参数=[
“授予类型”:“客户端凭据”
“客户端id”:“您的API密钥”
“客户机密”:“您的API机密”
]

既然您有一个
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"
让我们一件一件地说: 我们应该忘记接受编码、用户代理和接受语言头。稍后我将跳过它们

我们发现
-d
(数据进入httpBody)是错误的

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

然后我们得到:

$>-d "grant_type=client_credentials"
看起来好多了

我们看到
-u API:API\
对应于
.authenticate(用户:“API”,密码:“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
。另外,如果将curl放入终端中,它是否有效?编码
编码:JSONEncoding.default
不是JSON,因此使用
编码:URLEncoding(destination:.httpBody)
谢谢。不幸的是,amadeus服务器响应错误=“无效的请求”;“error_description”=“正文中只允许客户端id、客户端机密和授权类型参数”;title=“无效参数”;首先:你的解决方案正是我想要的。谢谢你。我编辑了您的答案并添加了API调用。不幸的是,它返回未知。有什么建议吗?顺便说一句,安全令牌存储.post方法中的值。不要尝试用其他问题编辑我的答案。而是创建一个新问题。对不起。提出了一个新问题
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"