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?
Tap in the top right of Facebook, then tap your name. Scroll down and tap Photos. Tap the album you'd like to view.
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.
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.
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.
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!)
}
}
})
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With