Facebook Page Image returning null in FBSDKGraphRequest
Asked Answered
T

3

6

I am attempting to retrieve the image from a facebook page. I have retrieved the page ID, and now I need to use this ID to get the image from the page. I am using the following code:

let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: ["fields" : "data"], HTTPMethod: "GET")
                            pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
                                if (error != nil) {
                                    print("picResult: \(result)")
                                } else {
                                    print(error!)
                                }
                            })

I am not getting an error, but my result is null. The picpath looks like this:

/110475388978628/picture

I copied the code directly from here but it isn't working. Any Idea what I'm doing wrong? I have the access token because I am able to get the page ID through the graph request

Tisatisane answered 8/5, 2016 at 15:55 Comment(0)
R
2

If you want to get the picture in the same request as the rest of the users information you can do it all in one graph request.

let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
request.startWithCompletionHandler({ (connection, result, error) in
    let info = result as! NSDictionary
    if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String {
        //Download image from imageURL
    }
})
Reptile answered 11/5, 2016 at 16:31 Comment(0)
P
1

there is no parameter called "fields". replace ["fields" : "data"] with nil

let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: nil, HTTPMethod: "GET")
                            pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
                                if (error != nil) {
                                    print("picResult: \(result)")
                                } else {
                                    print(error!)
                                }
                            })
Peptone answered 10/5, 2016 at 20:53 Comment(0)
P
1

You can use the Graph API explorer tool to help construct the graph request first: https://developers.facebook.com/tools/explorer

In this case, you'll see that you want the graph path to be just the object id (i.e., your picPath should be just 110475388978628 and your parameters should be [ "fields" : "picture"].

Then you'll want to parse the "url" out of the result["picture"]["data"]["url"].

Pharos answered 11/5, 2016 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.