Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook graph api. Get photos from albums

Please help me to make a correct request to Facebook api. Now i've got:

https://graph.facebook.com/me/albums?fields=photos

As the result I get very big Json with a lot of unnecessary info. I tried to do something like this:

https://graph.facebook.com/me/albums?fields=photos?fields=source,name,id

or like this:

https://graph.facebook.com/me/albums?fields=photos&fields=source,name,id

But graph api explorer, returned the same big response, or i caught an error. Any ideas how to do more compact response with only necessary info?

like image 812
Ivan Kozlov Avatar asked Feb 28 '12 17:02

Ivan Kozlov


People also ask

How do I pull up a photo album on Facebook?

Tap in the top right of Facebook, then tap your name. Scroll down and tap Photos. Tap the album you'd like to view.

What data can I get from Facebook Graph API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

Is Facebook Graph API deprecated?

Applies to all versions. Facebook Analytics will no longer be available after June 30, 2021. Additionally, we are deprecating the App Dashboard Plugin for Marketing API. For more information visit the Business Help Center.

How do I use Facebook Graph API and extract data?

To use the Graph API Explorer tool, go to developers.facebook.com/tools/explorer. Generate the access token you need to extract data from the Facebook Graph API by selecting Get User Access Token from the App Token drop-down menu under the name of your app.


2 Answers

GETTING ALBUM_ID

    if((FBSDKAccessToken.current()) != nil)
    {
        FBSDKGraphRequest(graphPath: "me/albums", parameters: ["fields" : "id"], httpMethod: "GET").start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil)
            {
                let data:[String:AnyObject] = result as! [String : AnyObject]
                self.arrdata = data["data"]?.value(forKey: "id") as! [String ]
            }
        })
    }

with above code you will get album_Id and then with that id we can get image like this : GETTING IMAGES FROM ALBUM_ID

    FBSDKGraphRequest(graphPath: "\(album_Id)/photos", parameters: ["fields": "source"], httpMethod: "GET").start(completionHandler: { (connection, result1, error) -> Void in
       if (error == nil)
       {
           let data1:[String:AnyObject] = result1 as! [String : AnyObject]
           let arrdata:[String] = data1["data"]?.value(forKey: "source") as! [String ]
           for item in arrdata
           {
               let url = NSURL(string: item )
               let imageData = NSData(contentsOf: url! as URL)
               let image = UIImage(data: imageData! as Data)
               self.imgArr.append(image!)
           }
        }
     })
like image 176
Rohan Dave Avatar answered Oct 07 '22 19:10

Rohan Dave


for Swift 5

first get albums id like this

func getAlbumsData()
{

        GraphRequest.init(graphPath: "me", parameters: ["fields":"id,name,albums{name,picture}"]).start(completionHandler: { (connection, userResult, error) in

            if error != nil {

                print("error occured \(String(describing: error?.localizedDescription))")
            }
            else if userResult != nil {
                print("Login with FB is success")
                print()



                let fbResult:[String:AnyObject] = userResult as! [String : AnyObject]

                self.albumsPhotos = (fbResult["albums"] as! [String:AnyObject])["data"] as? [[String:AnyObject]]
                self.tblFbAlbums.reloadData()




            }
        })
    }

then get albums image with this method

func fetchalbumsPhotosWithID() {


        let graphRequest : GraphRequest  = GraphRequest(graphPath: "\(album_Id)/photos", parameters: ["fields": "source"] )

        graphRequest.start(completionHandler: { (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                print("fetched user: \(result)")

                let data =  result as! [String:Any]


            }
        })

    }

album_Id is a number you get from getAlbumsData()

like image 36
balkaran singh Avatar answered Oct 07 '22 17:10

balkaran singh