获取电子邮件和姓名 Facebook SDK v4.4.0 Swift

48

TL;TR:如何使用Facebook SDK 4.4获取已登录应用程序的用户的电子邮件和姓名?

到目前为止,我已成功实现了登录,并且可以从应用程序的任何地方获取当前的访问令牌。

我的登录视图控制器和 Facebook 登录按钮的配置:

class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {

    @IBOutlet weak var loginButton: FBSDKLoginButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        if(FBSDKAccessToken.currentAccessToken() == nil)
        {
            print("not logged in")
        }
        else{
            print("logged in already")
        }

        loginButton.readPermissions = ["public_profile","email"]
        loginButton.delegate = self

    }

    //MARK -FB login
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        //logged in
        if(error == nil)
        {
            print("login complete")
            print(result.grantedPermissions)
        }
        else{
            print(error.localizedDescription)
        }

    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        //logout
        print("logout")
    }

现在我可以这样在我的主视图中获取访问令牌:

   let accessToken = FBSDKAccessToken.currentAccessToken()
    if(accessToken != nil) //should be != nil
    {
        print(accessToken.tokenString)
    }

我该如何获取已登录用户的姓名和电子邮件?我看到很多问题和答案都是使用旧版SDK或使用Objective-C。


https://dev59.com/dF0Z5IYBdhLWcg3wfge-#54668507 - Naresh
10个回答

75

我在安卓中使用了字段,所以我打算在iOS中也尝试一下,结果它也可以。

let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
   req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
       if(error == nil) {
            print("result \(result)")
       } else {
            print("error \(error)")
       }
   }
)

结果将会打印出来:

result {
   email = "email@example.com";
   id = 123456789;
   name = "Your Name";
}

发现这些字段与User端点相等,请参见此链接,您可以在其中查看所有可获取的字段。

适用于Swift 4及以上版本的更新

let r = FBSDKGraphRequest(graphPath: "me",
                          parameters: ["fields": "email,name"],
                          tokenString: FBSDKAccessToken.current()?.tokenString,
                          version: nil,
                          httpMethod: "GET")

r?.start(completionHandler: { test, result, error in
    if error == nil {
        print(result)
    }
})

针对Swift 5的FBSDKLoginKit 6.5.0更新

guard let accessToken = FBSDKLoginKit.AccessToken.current else { return }
let graphRequest = FBSDKLoginKit.GraphRequest(graphPath: "me",
                                              parameters: ["fields": "email, name"],
                                              tokenString: accessToken.tokenString,
                                              version: nil,
                                              httpMethod: .get)
graphRequest.start { (connection, result, error) -> Void in
    if error == nil {
        print("result \(result)")
    }
    else {
        print("error \(error)")
    }
}

4
过去三个小时我一直在为获取邮件而苦恼,他们真的需要更新文档以适应这些重大变化。谢天谢地我找到了这个。 - Isuru
@RageCompex,这对我也是一个救星。非常感谢。请问是否可以提供电话号码? - Tristan.Liu
请见更新的答案 @Tristan.Liu,电话号码不在列表中,但可能在您获得ID后的其他位置找到。我认为您还需要询问那个权限。 - CularBytes
1
我无法获取电子邮件。我甚至添加了一些其他字段,可以正确获取例如名字、姓氏、性别等,它们都能正常工作,但是电子邮件却无处可寻!!为什么? - Jesus Rodriguez
3
将其作为NSDictionary访问,例如 let r = result as! NSDictionary,使用例如 r["first_name"] 获取值。 - Elliott Davies
显示剩余3条评论

29
let request = GraphRequest.init(graphPath: "me", parameters: ["fields":"first_name,last_name,email, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)

request.start({ (response, requestResult) in
      switch requestResult{
          case .success(let response):
             print(response.dictionaryValue)
          case .failed(let error):
             print(error.localizedDescription)
      }
})

无法将类型为 '__NSCFDictionary' (0x195fd38a8) 的值转换为 'NSData' (0x195fd2750)。 - Yaroslav Dukal

18

对于Swift 3和Facebook SDK 4.16.0:

func getFBUserInfo() {
    let request = GraphRequest(graphPath: "me", parameters: ["fields":"email,name"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
    request.start { (response, result) in
        switch result {
        case .success(let value):
            print(value.dictionaryValue)
        case .failed(let error):
            print(error)
        }
    }
}

并将打印:

Optional(["id": 1xxxxxxxxxxxxx, "name": Me, "email": stackoverflow@gmail.com])

10

使用 GraphRequest 类,将检索用户的电子邮件名字姓氏id

// Facebook graph request to retrieve the user email & name
let token = AccessToken.current?.tokenString
let params = ["fields": "first_name, last_name, email"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params, tokenString: token, version: nil, httpMethod: .get)
graphRequest.start { (connection, result, error) in

    if let err = error {
        print("Facebook graph request error: \(err)")
    } else {
        print("Facebook graph request successful!")

        guard let json = result as? NSDictionary else { return }
        if let email = json["email"] as? String {
            print("\(email)")
        }
        if let firstName = json["first_name"] as? String {
            print("\(firstName)")
        }
        if let lastName = json["last_name"] as? String {
            print("\(lastName)")
        }
        if let id = json["id"] as? String {
            print("\(id)")
        }
    }
}

7
在通过Facebook登录后,请调用下面的函数。
   func getUserDetails(){

    if(FBSDKAccessToken.current() != nil){

        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email"]).start(completionHandler: { (connection, result, error) in

            guard let Info = result as? [String: Any] else { return }

             if let userName = Info["name"] as? String
                {
                   print(userName)
                }

        })
    }
}

5

获取Facebook iOS SDK用户姓名和电子邮件Swift 3

FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil) {
            let fbDetails = result as! NSDictionary
            print(fbDetails)
        } else {
            print(error?.localizedDescription ?? "Not found")
        }
    })

4
您可以使用此代码获取用户的电子邮件、姓名和个人资料图片。
   @IBAction func fbsignup(_ sender: Any) {
    let fbloginManger: FBSDKLoginManager = FBSDKLoginManager()
    fbloginManger.logIn(withReadPermissions: ["email"], from:self) {(result, error) -> Void in
        if(error == nil){
            let fbLoginResult: FBSDKLoginManagerLoginResult  = result!

            if( result?.isCancelled)!{
                return }


            if(fbLoginResult .grantedPermissions.contains("email")){
                self.getFbId()
            }
        }  }

}
func getFbId(){
if(FBSDKAccessToken.current() != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email,picture.type(large)"]).start(completionHandler: { (connection, result, error) in
    guard let Info = result as? [String: Any] else { return } 

            if let imageURL = ((Info["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
        //Download image from imageURL
    }
if(error == nil){
print("result")
}
})
}
}

4

这个框架似乎已经更新,我使用的方法是:

import FacebookCore

let graphRequest: GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"], accessToken: accessToken, httpMethod: .GET)

graphRequest.start({ (response, result) in
      switch result {
      case .failed(let error):
           print(error)
      case .success(let result):
           if let data = result as? [String : AnyObject] {
              print(data)
           }     
      }
})

2

In Swift 4.2 and Xcode 10.1

@IBAction func onClickFBSign(_ sender: UIButton) {

    if let accessToken = AccessToken.current {
        // User is logged in, use 'accessToken' here.
        print(accessToken.userId!)
        print(accessToken.appId)
        print(accessToken.grantedPermissions!)
        print(accessToken.expirationDate)

        let request = GraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,first_name,last_name,picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
        request.start { (response, result) in
            switch result {
            case .success(let value):
                print(value.dictionaryValue!)
            case .failed(let error):
                print(error)
            }
        }

        let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
        self.present(storyboard, animated: true, completion: nil)
    } else {

        let loginManager=LoginManager()

        loginManager.logIn(readPermissions: [ReadPermission.publicProfile, .email, .userFriends, .userBirthday], viewController : self) { loginResult in
            switch loginResult {
            case .failed(let error):
                print(error)
            case .cancelled:
                print("User cancelled login")
            case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                print("Logged in : \(grantedPermissions), \n \(declinedPermissions), \n \(accessToken.appId), \n \(accessToken.authenticationToken), \n \(accessToken.expirationDate), \n \(accessToken.userId!), \n \(accessToken.refreshDate), \n \(accessToken.grantedPermissions!)")

                let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
                request.start { (response, result) in
                    switch result {
                    case .success(let value):
                        print(value.dictionaryValue!)
                    case .failed(let error):
                        print(error)
                    }
                }

                let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
                self.navigationController?.pushViewController(storyboard, animated: true)

            }
        }
    }

}

完整详情请参考用户API参考手册。"Original Answer"翻译成"最初的回答"。

1
在Swift中,您可以从登录按钮的didCompleteWithResult回调中进行图形请求(如@RageCompex所示)。
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
    {
        print(result.token.tokenString) //YOUR FB TOKEN
        let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: result.token.tokenString, version: nil, HTTPMethod: "GET")
        req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
            if(error == nil)
            {
                print("result \(result)")
            }
            else
            {
                print("error \(error)")
            }
        })
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接