使用Alamofire的cURL请求

时间:2016-03-13 03:28:01

标签: ios swift curl alamofire

我正在尝试使用Alamofire来完成此cURL请求

curl -X POST -H "Content-Type: application/json" \
 --user "<client_id>:<client_secret>" \
 -d '{"grant_type": "client_credentials", "scope": "public"}' \
 'https://api.lyft.com/oauth/token'

我目前有:

  let plainString = "clientID:clientSecret" as NSString
    let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
    let base64String = plainData?.base64EncodedStringWithOptions([])

    let headers = [
        "Authorization": "Basic \(base64String)",
        "Content-Type": "application/json"
    ]

    let params = [
        "grant_type": "client_credentials",
        "scope": "public"
    ]

    Alamofire.request(.POST, "https://api.lyft.com/oauth/token", parameters: params, encoding: .JSON, headers: headers).responseJSON { response in
        debugPrint(response)
    }

这给了我一个状态400

我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:2)

解决了它,

必须使用.authenticate

Alamofire.request(.POST, "https://api.lyft.com/oauth/token", parameters: params, encoding: .JSON, headers: headers).authenticate(user: "clientID", password: "clientSecret").responseJSON { response in
        debugPrint(response)
    }