2016-09-17 135 views
1

我在代码中遇到了一些麻烦。我在“loginwithreadpermission”中授予了“user_photos”的许可,但仍未获得用户相册。我也试过这个“fbuserid/albums”,但是这对我也不适用。请帮助我做错了什么。无法访问当前登录Facebook用户的相册 - Swift

这是我的登录代码:

@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. 
        } 
       } 


      } 
+0

放“我/专辑” instaed的“ (FBID)/专辑”。 – ashmi123

+0

@ ashmi123我试过这个,但没有工作 –

+0

你得到了什么错误?你有没有在你的FB帐户或其他应用程序? – ashmi123

回答

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" 
    } 
} 
+0

我只需要当前用户的相册ID。因此,请忽略这些字段:电子邮件,名字。 –