Facebook Graph API GET请求 - 应包含"字段"参数(Swift,Facebook SDK v4.5.1)

时间:2015-08-16 03:58:11

标签: ios xcode facebook swift facebook-graph-api

我的iOS应用在注册过程中使用Facebook的图谱API请求获取用户信息,在升级到Facebook的最新SDK之前工作正常。升级后,我收到一个运行时错误说明:" FBSDKLog:从Graph API v2.4开始,对/ me的GET请求应该包含一个明确的"字段"参数"

以下是代码:

func requestFacebook() {

    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else if error == nil
        {
            let birthday : NSString = (result.valueForKey("birthday") as? NSString)!

            var currentDate = NSDate()
            var birthdayFormatter = NSDateFormatter()
            let userCalendar = NSCalendar.currentCalendar()
            birthdayFormatter.dateFormat = "MM/DD/YYYY"
            var birthdayNSDate = birthdayFormatter.dateFromString(birthday as String)

            var userAge = self.calculateAge(birthdayNSDate!)

            PFUser.currentUser()!["age"] = userAge

            var facebookID: NSString = (result.valueForKey("id") as? NSString)!
            var pictureURL = "https://graph.facebook.com/\(facebookID)/picture?type=large&return_ssl_resources=1"

            var URLRequest = NSURL(string: pictureURL)
            var URLRequestNeeded = NSURLRequest(URL: URLRequest!)

            NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in
                if error == nil {
                    var picture = PFFile(data: data)
                    PFUser.currentUser()!["picture"] = picture
                    PFUser.currentUser()!.saveInBackgroundWithBlock({ (success, error) -> Void in
                        if error == nil {

                            var userPicture:PFFile = PFUser.currentUser()!.valueForKey("picture") as! PFFile
                            userPicture.getDataInBackgroundWithBlock { (imageData, error) -> Void in
                                if error == nil {

                                    self.meProfileImageView.image = UIImage(data: imageData!)
                                    var userName:String = PFUser.currentUser()!.valueForKey("username") as! String
                                    var userAge:Int = PFUser.currentUser()!.valueForKey("age") as! Int
                                    self.meLabel.text = "\(userName), \(userAge)"

                                    self.findFriends()

                                }
                            }

                        } else {
                            println(error)
                        }
                    })
                }
                else {
                    println("Error: \(error.localizedDescription)")
                }
            })
        }
    })
}

错误消息位于以下行:

let birthday : NSString = (result.valueForKey("birthday") as? NSString)!

在升级到Facebook的SDK v4.5.1之前,此代码运行完全正常,但我现在还不知道要改变什么来使其工作。非常感谢能帮助我的人!

4 个答案:

答案 0 :(得分:26)

使用"字段"如键,然后用逗号分隔你的值

let params = ["fields": "email, friends"]

答案 1 :(得分:12)

在ObIn-C中插入logInWithReadPermissions方法:

parameters:@{@"fields": @"id, name, email"}

如果你在参数中留下nil,你只会在结果对象中收到id和name,尽管你已经设置了例如:

NSArray *permissionsArray = @[ @"email", @"public_profile"];

在文档中不是很清楚,希望它可以帮助。

答案 2 :(得分:6)

我遇到了同样的问题,但添加字段选项会在获取用户照片时生成请求错误。我通过在代码中添加一个简单的parameters:@{@"fields": @""}解决了这个问题:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"me/picture"]
                              parameters:@{@"fields": @""}
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    if (!error) {
        // success
    } else {
        // fail
    }}
];

答案 3 :(得分:4)

在Swift中,你可以声明多个这样的参数:

let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, first_name, last_name, gender, picture"])