SwiftyJD SwiftyJD - 1 year ago 235
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)
.responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")

guard let value = response.result.value else {


let swiftyJsonVar = JSON(value)

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


case .Failure(let error):

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





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

Answer Source

You are forgetting to pass the parameters.

Try this:

Alamofire.request(.POST, "\(baseTestOathURL)facebook", parameters: parameters, encoding: .JSON, headers: headers)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download