2017-05-08 125 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 
        } 
        } 
       }