如何使用 Alamofire (swift) 执行 post 以获得安全令牌作为 return 值
How to perform a post with Alamofire (swift) to get a security token as return value
我想使用 Swift Alamofire 通过 cURL 请求令牌。但是,到目前为止,我无法弄清楚如何以正确的顺序传递正确的参数以使其工作。
Amadeus 给出的描述如下:
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)
}
预期结果是在 return 中获取 JSON 文件,包括发出 API 请求的令牌。非常感谢任何帮助。谢谢
文档说参数应该包括 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"
]
必须作为正文传递的字符串是:
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"
]
让我们利用 Alamofire 的有用调试工具,因为您有一个 cURL
示例。
让我们破解您当前的代码:
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 headers。我稍后会跳过它们。
我们看到 -d
(httpBody 中的数据)是错误的。
让我们将其更改为:encoding: JSONEncoding.default
为 encoding: 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" \
那应该可以了。
顺序不一样,但服务器应该不会在意。它应该是 key/access 而不是 index/access.
所以最后:
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)
}
输出(跳过 headers):
$ 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"
我想使用 Swift Alamofire 通过 cURL 请求令牌。但是,到目前为止,我无法弄清楚如何以正确的顺序传递正确的参数以使其工作。
Amadeus 给出的描述如下:
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)
}
预期结果是在 return 中获取 JSON 文件,包括发出 API 请求的令牌。非常感谢任何帮助。谢谢
文档说参数应该包括 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"
]
必须作为正文传递的字符串是:
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"
]
让我们利用 Alamofire 的有用调试工具,因为您有一个 cURL
示例。
让我们破解您当前的代码:
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 headers。我稍后会跳过它们。
我们看到 -d
(httpBody 中的数据)是错误的。
让我们将其更改为:encoding: JSONEncoding.default
为 encoding: 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" \
那应该可以了。
顺序不一样,但服务器应该不会在意。它应该是 key/access 而不是 index/access.
所以最后:
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)
}
输出(跳过 headers):
$ 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"