iOS Facebook Graph Request multiple picture sizes - ios

I'm using Facebook's graph API and am trying to get two different picture sizes of a user's current profile picture. One picture I would like to be size 250x250, and the other picture I would like be size 1080x1080. Here is my current code:
let params = ["fields": "first_name, last_name, email, picture.width(1080).height(1080)"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
graphRequest.startWithCompletionHandler { (connection, result, error) in
if error != nil {
print(error)
}
}
This returns the URL to a picture of size 1080x1080 and works. Now if I change the params to add a request for a picture of size 250x250:
let params = ["fields": "first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)"]
I get this error basically saying I can't use the field picture more than once:
Syntax error "Field picture specified more than once. This is only possible before version 2.1" at character 94: first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)
Does this mean the only way to accomplish what I want is to make a batch request? And if that's the case, is there a function that gets called once both batch requests have completed?

There's another (better) way of doing this with just a single, non-batch request by using field aliasing, which I came across recently when I had the same issue.
In the OP's case the fields parameter would be set to:
first_name, last_name, email, picture.width(250).height(250).as(picture_small), picture.width(1080).height(1080).as(picture_large)
The response will then include two fields named picture_small and picture_large containing the respective picture data. E.g:
{
"first_name": "Mark",
"last_name": "Zuckerberg",
"picture_small": {
"data": {
"height": 320,
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/p320x320/12208495_10102454385528521_4749095086285673716_n.jpg?oh=e14ffdec03ceb6f30da80d1c4c5c4c02&oe=57254837",
"width": 320
}
},
"picture_large": {
"data": {
"height": 547,
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/12208495_10102454385528521_4749095086285673716_n.jpg?oh=b008ae35daeea9b0d9babbee9d701a34&oe=572336EC",
"width": 547
}
},
"id": "4"
}

This worked for me to get the large size images. Check the following query.
NSString * graphPath = [NSString stringWithFormat:#"%#?fields=photos.fields(id,link,source)", albumID];

You have two solutions:
the boring one is to make two requests. while the better solution is to make a batch request:
This is the cURL batch request to my profile, and it works:
curl -XPOST "https://graph.facebook.com/v2.4" -d "access_token=put_your_access_token_here" -i -d 'batch=[{"method" : "GET", "relative_url":"me?fields=first_name,last_name,picture.width(250).height(250)"},{"method" : "GET", "relative_url" : "me?fields=first_name,last_name,picture.width(1080).height(1080)"}]' -i
You just need to put your access token.

Related

Nested syntax to use on Facebook Graph API request

I want to receive a larger profile picture than the standard 50px one that returns.
I can receive email etc no problem, but how do I pass fields to indicate that I want a larger profile picture. I am clear that you can pass nested parameters to picture to return a larger one, but I cannot find information on how to write this when dealing with the Facebook iOS SDK.
extension LoginManagerLoginResult {
func graphData() {
let params = ["fields": "first_name, last_name, email, picture{height: 1000}"]
let graphRequest = GraphRequest(
graphPath: "me",
parameters: params,
tokenString: self.token!.tokenString,
version: nil,
httpMethod: .get
)
graphRequest.start { graph, any, error in
debugPrint(any)
debugPrint(error)
debugPrint(graph)
}
}
}
I had the syntax wrong. To get pictures off different sizes you can pass fields like below:
let params = ["fields": "first_name, last_name, email, picture.width(1000)"]
You can also pass:
picture.type(large)
or:
picture.height(300)
Or other height values as required.

How to get facebook feed with images and videos along with text?

I want to fetch logged user feeds within ios app, I have try with graph api that, graph api returns the data, its fine.
But when I have cross check the data to the actual facebook feed on facebook user page there are some problems found:
If post has text only than text coming on message key, along with post id,
If post has text with image/video only text coming on message key
And if post has only image/video than message key not coming.
I know message key is only for description/text but images? Is there any way to get the feed with whole content just same as appeared on facebook.
I have try below code:
For Login
FBSDKLoginManager().logOut()
FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile", "user_posts"], from: self) { (result, error) in
if error != nil {
print("failed to start graph request: \(String(describing: error))")
return
}
// self.getPosts()//old
self.getFBPost()
}
For getFBPost
func getFBPost() {
FBSDKGraphRequest(graphPath: "/me/feed", parameters: nil)?.start(completionHandler: { (fbConnectionErr, result, error) in
// print(fbConnectionErr)
print(result)
//print(error)
})
}
Coming response like this:
{
"data": [
{
"message": "https://medium.com/frame-io-engineering/huge-images-small-phone-15414b30d39e",
"created_time": "2018-12-03T13:59:01+0000",
"id": "68288174406_653966194"
},
{
"created_time": "2018-12-01T13:43:02+0000",
"id": "68288174406_6528518443724"
},
{
"message": "I love my Mom",
"created_time": "2018-11-30T13:27:38+0000",
"id": "68288174406_652289420323"
}
}
as you can see the second post has only id and created time, while we check this on facebook page this the post with image, and third post has video but only text coming from graph api
Please guide whats wrong I did? Or What should I do to getting whole feed data in json formate just same as appeared on facebook?
You are not asking for any fields in the API call, so you only get default ones. It is called "Declarative Fields" (/me?fields=message,created_time,...) and came with v2.4:
https://developers.facebook.com/docs/graph-api/changelog/archive#v2_4_new_features
https://developers.facebook.com/docs/graph-api/using-graph-api/#reading
I have read the give doc that are shared by #luschn. And got the solution I forgot the declarative fields as #luschn suggest me, Now solution given below: Only need to change in the graph api,
FBSDKGraphRequest(graphPath: "/me/feed", parameters: ["fields":"created_time,attachments,type,message"])?.start(completionHandler: { (fbConnectionErr, result, error) in
print(fbConnectionErr)
print(result)
print(error)
let re = result as? [String: Any]
let data = re?["data"] as! [[String: Any]]
for dict in data {
print(dict)
}
})
OR
If you have page id you can use below url to hit the get api.
https://graph.facebook.com/(api_version)/(page_id)?fields=feed{created_time,attachments,message}&access_token=(token_id)

Get insights/page_fans for a Facebook page from Facebook's open graph

I'm trying to get the number of fans that a music artist has on their Facebook page, but it's not working. I've combed through the FBAPI docs as well as SO and still nothing. Here's my code:
func getHolychildInfo() {
//Make request
let newGraphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/holychildmusic/insights/page_fans", parameters: ["period" : "lifetime", "show_description_from_api_doc" : "true", "fields": "read_insights"], httpMethod: "GET")
newGraphRequest.start(completionHandler: { (connection, result, error) in
if ((error) != nil) {
print("Holychild error getting insights: \(error.debugDescription)")
} else {
print("\nHolychild insights result:\n\n\(result)")
}
})
}
Here's my result:
data = (
);
paging = {
next = "https://graph.facebook.com/v2.8/holychildmusic/insights/page_fans?access_token=EAAIB5k3aWEEBAHBD9lZC5AAzZAVV8K8CGBfqaxcrLdZA7oZB2Gdar8cQphXj4VciloZAnZBKp5ZA59BmGloSNz847nFqZCTVsYZCl9rrOk88OnfCnDwwADKnkOO5EUhGumEbW96riHplgfBLdnZAEYmB2Qz4ZAH1sWbuftmGKDqPft4l5QAHSZAimIyI6sOHaKWiurRK201Af6NQCXGliZBsZAUYosUHttkUbo4CQZD&fields=read_insights&format=json&include_headers=false&period=lifetime&sdk=ios&show_description_from_api_doc=true&since=1487457711&until=1487716911";
previous = "https://graph.facebook.com/v2.8/holychildmusic/insights/page_fans?access_token=EAAIB5k3aWEEBAHBD9lZC5AAzZAVV8K8CGBfqaxcrLdZA7oZB2Gdar8cQphXj4VciloZAnZBKp5ZA59BmGloSNz847nFqZCTVsYZCl9rrOk88OnfCnDwwADKnkOO5EUhGumEbW96riHplgfBLdnZAEYmB2Qz4ZAH1sWbuftmGKDqPft4l5QAHSZAimIyI6sOHaKWiurRK201Af6NQCXGliZBsZAUYosUHttkUbo4CQZD&fields=read_insights&format=json&include_headers=false&period=lifetime&sdk=ios&show_description_from_api_doc=true&since=1486939311&until=1487198511";
};
As you can see, there is nothing in the "data" part of the response. The "page_fans" insights metric is supposed to return a number - among other things - but instead returns nothing.
All insights metrics besides the two public ones (page_fans_country and page_storytellers_by_country) require admin access to the page (admin user or page access token with read_insights permission.)
But the fan_count field of the page object is public, so just request that:
https://developers.facebook.com/tools/explorer/?method=GET&path=holychildmusic%3Ffields%3Dfan_count&version=v2.8

Locale-independent FBSDKGraphRequest

I am interested in fetching user's gender from Facebook. The device locale is set to Russian. Code:
let FBRequest = FBSDKGraphRequest(
graphPath: "me",
parameters: [
"fields": "id, gender",
"locale": "en_US"
]
)
FBRequest.startWithCompletionHandler(
{
(connection, result, error) -> () in
if error == nil {
print(result["gender"])
}
}
)
It prints "мужской" instead of "male". How can I get response ignoring the device locale? Thank you in advance!
I've had the same problem as you. I'm trying to change the locale putting a key and value as "locale":"pt_BR" or "locale":"en_US". Both fail.
The only way I found out to resolve is by changing the line below in the file "FBSDKGraphRequestConnection.m" (I installed last version SDK trough cocoapods) in the method called: - (NSString *)urlStringForSingleRequest:(FBSDKGraphRequest *)request forBatch:(BOOL)forBatch
request.parameters[#"locale"] = [NSLocale currentLocale].localeIdentifier;
to for example:
request.parameters[#"locale"] = #"en_US";
The SDK gets the current NSLocale of the device and overrides the parameter you put on it.

Get objects with request and mapping progress in RestKit

Let's say I have an api like the follwing:
//http://example.com/api/comment/
response:
[
{
"id": 1,
"username": "some_user",
"content": "content of the comment"
},
...
{
"id": 9999,
"username": "some_other_user",
"content": "content of the comment"
}
]
It is critical that I fetch all the objects in one request.
Since RestKit appears to be slow when mapping responses with many objects into core data the following takes very very long (more than a minute in the simulator).
RKObjectManager.sharedManager().getObjectsAtPath("/api/comment/", parameters: nil, success: { (operation, result) -> Void in
}) { (operation, error) -> Void in
}
Since I couln't find a way to speed up the request/mapping I was hoping to be able to display a progress bar for the duration of this operation. Is it possible to get a progress block for the mapping with someting like objectsToMap and mappedObjects?

Resources