无法访问当前登录Facebook用户的相册 - Swift

时间:2016-09-17 08:52:24

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

我的代码遇到了一些麻烦。我允许" user_photos" in" loginwithreadpermission"但仍未获得用户专辑。我也试过这个" fbuserid / albums"但这也不适合我。请帮我解决我的错误。

这是我的登录代码:

@IBAction func facebookLogin(sender: AnyObject)
{


    let facebookLogin = FBSDKLoginManager()
    facebookLogin.logOut()
    facebookLogin.logInWithReadPermissions(["email" , "user_photos"], fromViewController: nil) { (facebookResult, facebookError) in


        if facebookError != nil {
            print("Login Failed. Error: \(facebookError)")
        }
        else {
            if !(facebookResult.isCancelled)
            {

                let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
                if accessToken != nil
                {
                    token = accessToken

                    let credential = FIRFacebookAuthProvider.credentialWithAccessToken(accessToken)
                    print("1")
                    FIRAuth.auth()?.signInWithCredential(credential)
                    {
                        (user, error) in

                        if error != nil
                        {
                            print("Error Firebase: \(error)")
                        }
                        else
                        {


                            if self.userEmails.contains(user!.email!)
                            {
                                print("User Already Exist")

                                NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: KEY_UID)
                                USER_UID = (user?.uid)!
                                print("id:\(USER_UID)")
                                LOGIN_FLAG = 1
                                FaceBookFlag = 1

                                self.performSegueWithIdentifier("fbLogin", sender: nil)

                            }
                            else if user!.email != nil
                            {
                                print("Creating a User Data in FB")

                                var request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"email,first_name,age_range"]);
                                    request.startWithCompletionHandler({ (connection, result, error) in


                                        if error == nil
                                        {
                                            print("res: \(result)")
                                            if let dob = result.valueForKey("age_range") as? [String: AnyObject]
                                            {
                                                print("dd: \(dob["min"]!)")
                                                self.dateOfBirth = String(dob["min"]!)
                                                print("dob: \(dob)  , date: \(self.dateOfBirth)")
                                                print(" date: \(self.dateOfBirth!)")

                                            }
                                        }
                                        else
                                        {
                                            print("error:: \(error)")
                                        }




                                    let displayName = user?.displayName!.componentsSeparatedByString(" ")
                                    let firstName = displayName![0]
                                    let lastName = displayName![1]
                                        print("url: \(user?.photoURL)")
                                    let profilePicUrl = user?.photoURL
                                    let picture = String(profilePicUrl!)
                                    let userEmail = user!.email!

                                    let _user = ["username": "\(firstName+lastName)","emailAddress": "\(userEmail)", "dateOfBirth": "\(self.dateOfBirth!)"]

                                    ref.child("users").child(user!.uid).setValue(_user)
                                    ref.child("users").child(user!.uid).child("images").setValue([picture])



                                    NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: KEY_UID)
                                    USER_UID = (user?.uid)!
                                    LOGIN_FLAG = 1
                                    FaceBookFlag = 1


                                    //Segue to reveal view controller
                                    self.performSegueWithIdentifier("fbLogin", sender: nil)
                                })
                            }

                        }
                    }
                }
            }
        }
    }

}

这是我的代码,我要求给我当前用户的相册:

func getAlbums()
{

    //for sake of running i hardcoded the FBuserId.
    let fbid = "758111074330935"



     //169682503464671/photos
    let graphRequest = FBSDKGraphRequest(graphPath: "/\(fbid)/albums", parameters: ["fields":"photos,picture"] , HTTPMethod: "GET")




        graphRequest.startWithCompletionHandler { (connection, result, error) in

            if error != nil
            {
                print(error)
            }
            else
            {
                print("rr:\(result)")
                let value = result.valueForKey("data")
                print("value:\(value) ... \(result["data"])")

                if let graphData = result.valueForKey("data") as? [AnyObject]
                {
                    print("array of Any")

                    for obj in graphData
                    {
                        print("id: \(obj.valueForKey("id")!)")
                        let id = String(obj.valueForKey("id")!)
                        self.albumsId.append(id)

                        //Also save the album link here to.
                    }
                }


            }

1 个答案:

答案 0 :(得分:1)

此类请求有一组错误的字段。 请查看此文档以验证可用的字段:https://developers.facebook.com/docs/graph-api/reference/v2.7/album

特别是,我收到以下错误(自行解释):

{
  "error": {
    "message": "(#100) Unknown fields: email,first_name.",
    "type": "OAuthException",
    "code": 100,
    "fbtrace_id": "G0POfi9dWkb"
  }
}