Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

swift - IOS Facebook SDK: login doesn't return email despite permissions granted

I'm trying to get information through the facebook sdk but so far I'm getting only the id and the name of the user, although I'm sure there is an email available, as it is my account. I've been looking at several answer here but none of them solved my problem so far. What am I doing wrong, and why is it not returning more data as I am requesting several permissions?

Here is my code:

fbLoginManager.logInWithReadPermissions(["public_profile", "email", "user_friends", "user_about_me", "user_birthday"], handler: { (result, error) -> Void in
        if ((error) != nil)
        {
            // Process error
            println("There were an error: (error)")
        }
        else if result.isCancelled {
            // Handle cancellations
            fbLoginManager.logOut()
        }
        else {
            var fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                println(fbloginresult)
                self.returnUserData()
            }
        }
    })

And the function to get the user data:

func returnUserData()
{
    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)")

            if let userName : NSString = result.valueForKey("name") as? NSString {
                println("User Name is: (userName)")
            }
            if let userEmail : NSString = result.valueForKey("email") as? NSString {
                println("User Email is: (userEmail)")
            }
        }
    })
}

Which returns:

fetched user: {
id = 55555555;
name = "Kali Aney";
}
User Name is: Kali Aney
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Facebook Graph API broke it’s backward compatibility (when used in a default fashion) Since Graph-API version 2.4 (Pod Version 4.4.0).

FB Graph-API 2.4 does NOT return all default fields for user

To resolve this you can either use explicitly graph version 2.3:

[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil tokenString:[FBSDKAccessToken currentAccessToken].tokenString version:@"v2.3" HTTPMethod:nil]

in which case FB assures v2.3 will be available at least 2 years from now. https://developers.facebook.com/docs/graph-api/overview:

The Graph API has multiple versions available to access at any one time. Each version contains a set ofcore fields and edge operations. We make a guarantee that those core APIs will be available and un-modified in that version for at least 2 years from release. The platform changelog can tell you which versions are currently available.

OR

you can use new Graph-API (v2.4) in by asking for specific fields you are interested in:

[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields" : @"email,name"}]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...