在 FedEx OAuth Swift 中收到错误的请求错误

Getting bad request error in FedEx OAuth Swift

我正在尝试授权 FedEx 令牌,但出现 400 错误。

联邦快递开发人员linkhttps://developer.fedex.com/api/en-us/catalog/authorization/v1/docs.html#operation/API%20Authorization

代码段:

let headers = [
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        
        let parameters = [
            "grant_type":"client_credentials",
            "client_id":"***********************",
            "client_secret":"***********************"
        ] as [String : Any]
        Alamofire.request("https://apis-sandbox.fedex.com/oauth/token", method: .post, parameters:parameters,encoding: JSONEncoding.default, headers: headers).responseJSON {
            response in
            switch response.result {
            case .success:
                print(response)
                
                break
            case .failure(let error):
                
                print(error)
            }
        }

获取错误:

{
    errors =     (
                {
            code = "BAD.REQUEST.ERROR";
            message = "Missing or duplicate parameters. Please modify your request and try again.";
        }
    );
    transactionId = "b1d9d540-ed29-49fd-a4c2-907718e918c2";
}

从 FedEx 文档中您可以看到参数需要作为 form-urlencoded 发送。

确实,您在 headers 中指定了这一点,但随后您使用了 JSON 编码器,因此将发送 JSON 文档。

而是使用

 Alamofire.request("https://apis-sandbox.fedex.com/oauth/token",method: .post, parameters: parameters, encoder: URLEncoding.default,  headers: headers)