Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL request using Alamofire

I am trying to use Alamofire to complete this cURL request

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'

I currently have:

  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)
    }

This is giving me a status 400

What am I doing wrong?

Thanks in advance!

like image 815
Phil Avatar asked Aug 07 '26 16:08

Phil


1 Answers

Solved it,

Have to use the .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)
    }
like image 75
Phil Avatar answered Aug 10 '26 08:08

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!