SwiftyJD SwiftyJD - 5 months ago 79
Swift Question

How to use Alamofire with Facebook Login?

After retrieving an access token from Facebook, I'm trying to use the token to connect with an OAth2 server. When I try and connect I keep getting a 401 Unauthorized access code but when the backend developers connect it works.
Here is the Curl from the backend developers that works:

curl -X POST -H "Content-Type: application/json" -d '{
"client_id" : "1",
"access_token" : "EAARDjZAZBQnEEBAAxLZAE08npCWTeXUTQIHnP5rKx4poRZAlBAhL7UW6RJIUhZBmomIaIoNm97ERmVi0g2Ql5rfLZBckWf31hnOV6TCHkI8Tu4IBNknFBMsbaI8NvPl7aOp2qHhaAKaHx4UkwGJ9Eo8sZAaJhN7v0M7aCwNpUE3HgZDZD"
}' "http://localhost:3000/auth/facebook"


Here is my alamo fire code:

func facebookAPILogin (fbAccessToken: String, completionHandler: (ErrorType?) -> ()) {


let headers = [

"Content-Type": "application/json"

]

let parameters: [String: AnyObject] = [

"client_id" : "1",
"access_token" : fbAccessToken

]


Alamofire.request(.POST, "\(baseTestOathURL)facebook", encoding: .JSON, headers: headers)
.validate()
.responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")

guard let value = response.result.value else {

completionHandler(response.result.error)
return
}

let swiftyJsonVar = JSON(value)


print("This is the FaceBook response \(swiftyJsonVar)")

completionHandler(nil)

case .Failure(let error):

print("Sorry there was an error: \(error)")

completionHandler(error)

return
}

}

}


I can't figure out hwy I can't connect, any help will be appreciated.

Answer

You are forgetting to pass the parameters.

Try this:

Alamofire.request(.POST, "\(baseTestOathURL)facebook", parameters: parameters, encoding: .JSON, headers: headers)