2017-05-08 131 views
0

我有一位用户通过Firebase身份验证进行身份验证。我可以得到他们的显示名称,但想知道如何才能得到他们的名字。我是否必须使用某种令牌来调用FB Graph API?如何获取Firebase用户的名字

真诚 Vishaal

回答

0

是的,你需要获得一个令牌,基本上可以让你获得一个用户的Facebook的细节。

一旦获得验证令牌,您就可以使用FB Graph API来提取基本信息。

这里是一个示例代码。

let credential = FIRFacebookAuthProvider.credential(withAccessToken: accessToken.tokenString) 
     FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in 
      if let error = error 
       { 
        print("Login error: \(error.localizedDescription)") 
        let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert) 
        let okayAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) 
        alertController.addAction(okayAction) 
        self.present(alertController, animated: true, completion: nil) 

        return 
       } 
      if((FBSDKAccessToken.current()) != nil) { 
       var request = FBSDKGraphRequest(graphPath: "me", parameters:["fields":"first_name,last_name,email"], httpMethod: "GET") 
       request?.start(completionHandler: {(connection,result,error) -> Void in 
       if (error == nil) 
       { 
        var dictionary_user_info = result as? NSDictionary 
        let v_firstname = dictionary_user_info?.object(forKey: "first_name") as! String 
        let v_lastname = dictionary_user_info?.object(forKey: "last_name") as! String 
        let v_email = dictionary_user_info?.object(forKey: "email") as! String 
        } 
        } 
       }