David Seek David Seek - 1 month ago 12
iOS Question

How to retrieve Email from Facebook SDK

Note: please do not close as duplicate, I went through almost every existing thread here on Stackoverflow, but my problem is still not solved.

This is my Swift 3 function with the latest Facebook SDK:

let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: token?.tokenString, version: nil, httpMethod: "GET")
_ = req?.start(completionHandler: { (connection, result, error : Error?) -> Void in

if(error == nil) {
print("result \(result)")
} else {
print("error \(error!)")
}
})


Result:

result Optional({
id = 102080884567XXXXX;
name = "David Seek";
})


Email permission is approved:

enter image description here

Also the App is in live mode:

enter image description here

I have checked the reference guide, but I can't find my mistake.

My problem is, that I'm saving the facebookID as Email adress into my backend and I'm trying to figure out why... Therefore I have tried the provided code on top, but I'm not receiving the Email.

let result = FBSDKApplicationDelegate.sharedInstance().application(application,
open: url,
sourceApplication: sourceApplication,
annotation: annotation)
if result {

let token = FBSDKAccessToken.current()
let fieldsMapping = [
"id" : "facebookId",
"name" : "name",
"birthday": "birthday",
"first_name": "fb_first_name",
"last_name" : "fb_last_name",
"gender": "gender",
"email": "email"
]

backendless?.userService.login(withFacebookSDK: token, fieldsMapping: fieldsMapping, response: { (user: BackendlessUser?) in

}, error: { (fault: Fault?) -> Void in
print("Server reported an error: \(fault)")
})
}


I'm receiving every information, but the Email...

What am I missing? Help is very appreciated.

Answer

Email permission is granted:

That is not what that screenshot shows. It shows that it is approved, meaning your app can ask users for it. But it still has to ask.

Make sure that has happend successfully - either by debugging the access token, or via an API call to /me/permissions

You need to ask the user for permission during login.