1
votes

Can somebody help me with this?

My app, registered on Facebook and set as "live" has an appID, a secret and a Token client. From my app, userA performs a login on Facebook and I get IdA (which is app-scoped). From my app, userB performs a login on Facebook and I get IdB still app-scoped.

Now, whenever I try to get user profile picture I get an error. This is what I tried for user A, same for user B:

https://graph.facebook.com/IdA/picture?type=small&access_token=appID|secret "message": "Unsupported get request. Object with ID 'IdA' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api", "type": "GraphMethodException", "code": 100, "error_subcode": 33

https://graph.facebook.com/IdA/picture?access_token=appID "message": "Invalid OAuth access token.", "type": "OAuthException", "code": 190 https://graph.facebook.com/IdA/picture?access_token=Token Client "message": "Invalid OAuth access token.", "type": "OAuthException", "code": 190

Can somebody shed some light on this? Thanks in advance

1

1 Answers

0
votes

You need to give permissions and after that, you need to call graph API (FBSDKGraphRequest) to get the profile images. I have prepared a class, You can achieve your requirement from this class:

import UIKit
import FBSDKCoreKit
import FacebookCore
import FacebookLogin

class LoginWithFacebook {

    // MARK: - Properties
    static let sharedInstance = LoginWithFacebook()


    /**
     Takes user to the facebook login page so that user could login with his facebook credentials and brings back basic details of the user and photo album
     - parameter viewControl: controller on which the facebook page will be opened
     - parameter completion: on completion the method returns user info in the form of dictionary
     */
    public func fBLoginWithPhotoAlbum(viewControl: UIViewController, completion: @escaping ([String: Any]) -> Void) {

        var responseDictionary: [String: Any] = [String: Any]()
        let loginManager = LoginManager()

        loginManager.logIn(readPermissions: [.publicProfile, .email, .userFriends, .custom("user_birthday")], viewController: viewControl) { (loginResult) in
            print(loginResult)
            switch loginResult {
            case .failed(let error):
                print(error)

            case .success(grantedPermissions: _, declinedPermissions: _, token: let accessToken) :

                responseDictionary.updateValue(accessToken.authenticationToken, forKey: "accessToken")

                let param = ["fields": "first_name, last_name, picture.width(9999), email, friends, gender, age_range, birthday, photos{album,picture}"]
                FBSDKGraphRequest.init(graphPath: "me", parameters: param).start { (_, result, _) -> Void in

                    if let resultDictionary: NSDictionary = result as? NSDictionary {
                        responseDictionary.updateValue(resultDictionary, forKey: "userInfo")

                        FBSDKGraphRequest.init(graphPath: "me/albums", parameters: ["fields": "photos{picture}"], httpMethod: "GET").start(completionHandler: { (_, result, _) in

                            let data = (result as? NSDictionary)?.value(forKey: "data") as? NSArray
                            if (data?.count)! > 0 {
                                var profileAlbum: [Any] = data!.filter { NSPredicate(format: "(name == %@) ", "Profile Pictures").evaluate(with: $0) }
                                if profileAlbum.count > 0 {
                                    if let profilePicAlbum = profileAlbum[0] as? NSDictionary {
                                        let graphPath = profilePicAlbum.value(forKey: "id")

                                        //picture, "photos{link}"
                                        let param = ["fields": "photos{picture}"]
                                        FBSDKGraphRequest.init(graphPath: graphPath as? String, parameters: param, httpMethod: "GET").start(completionHandler: { (_, result, _) in
                                            if let profilePicArray = (result as? NSDictionary)?.value(forKeyPath: "photos.data") {
                                                responseDictionary.updateValue(profilePicArray, forKey: "profilePics")
                                                completion(responseDictionary)
                                            }
                                        })
                                    }
                                }
                            } else {
                                completion(responseDictionary)
                            }
                        })

                    } else {

                    }
                }

            case .cancelled :
                completion(["message": "Something Went Wrong"])
            }

        }
    }
}