Facebook ios sdk Graph request allows only one field as parameter

Viewed 613

I'm developing an app which requires facebook login, and user details. However, the same code that I used in previous apps for fb login doesn't work, and I need to make separate graph requests for each field like name, email, picture, etc.

Basically, whenever I try to do the following:

connection.add(GraphRequest(graphPath: "/me", parameters:["fields":"email, name, picture"])) { httpResponse, result in
                switch result {
                case .success(let response):
                    guard let userInfo = response.dictionaryValue else {
                        return
                    }
                    userDict["email"] = userInfo["email"]! as Any
                    UserDefaults.standard.set(userDict["email"], forKey: userKeys.email)
                case .failed(let error):
                    print("Graph Request Failed: \(error)")
                }
            }

I get this error:

Graph Request Failed: Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=400, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Syntax error "Expected end of string instead of "%"." at character 5: email%2C%20name%2C%20picture, com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=2500, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body =     {
    error =         {
        code = 2500;
        "fbtrace_id" = "FIc1yiUL+1z";
        message = "Syntax error \"Expected end of string instead of \"%\".\" at character 5: email%2C%20name%2C%20picture";
        type = OAuthException;
    };
};
code = 400;

And a similar error when I do:

connection.add(GraphRequest(graphPath: "/me", parameters:["fields":"picture.type(large)"]))

However, it doesn't give an error when I put in a single field as a parameter.

Any help would be greatly appreciated :)

2 Answers

I too got the same error when trying with the latest release. I suggest you to downgrade to the previous version. For me below versions are working fine:

pod 'FBSDKCoreKit', '~> 4.38.0' 
pod 'FBSDKLoginKit', '~> 4.38.0' 

After that run

pod deintegrate

pod install

Try this

    let loginManager = FBSDKLoginManager()
    loginManager.logOut()
    loginManager.logIn(withReadPermissions: ["public_profile", "user_photos" ,"user_videos"], from: self) { (result, error) in
        if error != nil {

        } else if (result?.isCancelled)! {
            print("Cancelled")
        } else {               
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "email, id, name"])
            graphRequest.start(completionHandler: { (connection, result, error) -> Void in
                if ((error) != nil) {
                    print("Error: \(String(describing: error))")
                } else {
                    var userName:String?
                    var userID:String?
                    var userIEmail:String? = ""
                    if let user : NSString = (result! as AnyObject).value(forKey: "name") as? NSString {
                        userName = "\(user)"
                    }
                    if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
                        userID = id as String
                        if userID == UserDefaultsHelper.username{
                            self.ClearUserData = false
                        }
                    }
                    if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
                        userIEmail = email as String
                    }
                }
            })
        }
    }
Related