1

I have been trying this code to get the user information a needed for my iOS app, when I press the func loginButton I should login and get the user name and email,

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if( error != nil)
    {
        println(error.localizedDescription)
        return
    }

    if let userToken = result.token{
        //Get user acces token 
        let token:FBSDKAccessToken=result.token

        println("Token = \(FBSDKAccessToken.currentAccessToken().tokenString)")

        println("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")


        let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
        let protectedPageNav = UINavigationController(rootViewController: protectedPage)

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        appDelegate.window?.rootViewController = protectedPageNav

    }

    //Show user information
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)

    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("fetched user: \(result)")
            let userEmail : NSString = result.valueForKey("email") as! NSString
            println("User Email is: \(userEmail)")
            let userName : NSString = result.valueForKey("name") as! NSString
            println("User Name is: \(userName)")

        }
    })
}

There is a comment show user information that it should print the email and user name, it prints the name but not the email and i get the following error: FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter,, can any one knows what am i doing wrong? thank you

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
altexo1
  • 13
  • 1
  • 4

3 Answers3

4

I think you would need to add the parameters.

Try changing it to:

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
Francis Yeap
  • 284
  • 2
  • 10
1

i have also the same issue and i have found this solution for objective c

 NSMutableDictionary*params = [NSMutableDictionary dictionary];
[params setValue:@"fields" forKeyPath:@"fields"];
[params setValue:@"name" forKeyPath:@"name"];
[params setValue:@"email" forKeyPath:@"email"];

and set the value of parameters to params this would work

shafi
  • 426
  • 3
  • 15
1

Add LoginButtonDelegate Delegate and add protocol, after that add below code and test

 func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
        print(loginButton)
        let loginManager = LoginManager()
        print(result?.token?.tokenString) //YOUR FB TOKEN
        let req = GraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: result?.token?.tokenString, version: nil, httpMethod: HTTPMethod(rawValue: "GET"))
        req.start { (connection, resultData, err) in
            if err == nil {
                print(resultData)
            }else {
                print(err)
            }
        }

           loginManager.logIn(permissions: ["email"], from: self) { (result, error) in
             if error != nil {
              // self.showPopup(isSuccess: false)
               return
             }
             guard let token = AccessToken.current else {
               print("Failed to get access token")
           //    self.showPopup(isSuccess: false)
               return
             }

             FirebaseAuthManager().login(credential: FacebookAuthProvider.credential(withAccessToken: token.tokenString)) {[weak self] (success) in
          //     self?.showPopup(isSuccess: true)
             }
           }
    }

    func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
         print(loginButton)
    }
NavinBagul
  • 741
  • 7
  • 24