我在这里看到过类似的问题,但似乎没有解决我的问题。
我目前正在创建一个使用Facebook SDK登录并返回用户信息的iOS应用。
我已成功登录Facebook并收到所需信息。但是,尽管允许电子邮件具有读取权限,但请求仍未返回电子邮件。
/********************
Place Facebook login button
*********************/
let loginButton = FBSDKLoginButton()
view.addSubview(loginButton)
loginButton.delegate = self
loginButton.readPermissions = ["email", "public_profile"]
//add button constraints
loginButton.center = view.center
}
/**
* Functions called when user handed back from facebook login
**/
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Did log out of facebook")
}
/**
* Functions called when user handed back from facebook login
**/
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil {
print(error)
} else
{
print("Successfully logged in with facebook")
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start
{ (connection, result, err) in
if err != nil{
print("Failed to start graph request: ", err)
return
}
print(result)
}
}
}
答案 0 :(得分:0)
您无法根据Facebook Graph API tool从FBSDKGraphRequest获取用户电子邮件。 Facebook仅返回用于登录的令牌。如果您使用Facebook登录Firebase,则可以使用CurrentUser.currentEmail方法获取电子邮件。
let login = FBSDKLoginManager()
login.logIn(withReadPermissions: ["email","user_posts","public_profile"], from: self, handler: {
(result: FBSDKLoginManagerLoginResult?, error: Error?) in
if error != nil {
print("Process error")
return
}
else if (result?.isCancelled)! {
print("Cancelled")
}
else {
//Success!
let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
self.firebaseLogin(credential)
}
})
答案 1 :(得分:0)
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
@IBAction func btnFbPressed(_ sender: Any) {
let loginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (loginResult, error) in
if error != nil {
print(error!)
}
else {
if (loginResult?.grantedPermissions.contains("email"))! {
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
UserDefaults.standard.set(FBSDKAccessToken.current().tokenString, forKey: "fbToken")
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil) {
print(result as Any)
let dictionary = result as! NSDictionary
print(dictionary)
let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
hud.tintColor = .black
hud.mode = .indeterminate
hud.label.text = "Loading"
hud.animationType = .fade
hud.show(animated: true)
let postString = String(format: "login_type=facebook&email=%@&id=%@&device=0", arguments: [dictionary.value(forKey: "email") as! String, dictionary.value(forKey: "id") as! String])
}
})
}
}
答案 2 :(得分:0)
我想了很久,发现this link暗示如果您没有确认的电子邮件地址,我不会在代码的登录部分找回它。
let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: "2.12")
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue!)
self.responseJSON = JSON(value.dictionaryValue!)
let fullName = self.responseJSON["first_name"].stringValue
let firstName = self.responseJSON["first_name"].stringValue
let lastName = self.responseJSON["last_name"].stringValue
let email = self.responseJSON["email"].stringValue
let idFb = self.responseJSON["id"].stringValue
let picture = self.responseJSON["picture"]["data"]["url"].stringValue
print("user id: \(idFb), firstName: \(firstName), fullname: \(fullName), lastname: \(lastName), picture: \(picture), email: \(email)")
print("ALL FIELDS PUBLISHED")
if let delegate = self.responseDelegate {
delegate.responseUpdated(self.responseJSON, forLogin: "FB")
}
case .failed(let error):
print(error)
}
}
}
因此,当我到达并确认我的电子邮件地址时,它给了我电子邮件作为回应。这是一些附带的快照。
这就是我的方法。后来,我实现了如果未返回电子邮件的情况,我显示了一个警报,警告用户该Facebook帐户没有与之关联的电子邮件,然后他们确认其电子邮件或通过在TextField Alert中提供单独的电子邮件地址来继续。就是这样。