I am implementing the Checkins Facebook Graph API using Facebook SDK. This is the code for Checkins
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:accsstoken,#"access_token",#"253651184683030",#"place",#"I m here in this place",#"message",#"30.893075018178,75.821777459326",#"coordinates", nil];
[FBRequestConnection startWithGraphPath:#"/me/checkins"
parameters:dict
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Error...%#",error);
}];
When I tried this above code. It gives me following error:
error = {
code = 160;
message = "(#160) Invalid coordinates. Coordinates must contain at least latitude and longitude.";
type = OAuthException;
};
It gives the coordinates issue. Is there a different way to pass the coordinates parameters? Please help me out of this issue.
As far as I know checkins are deprecated and you should use post with place parameter.
And here is the link. Facebook SDK reference
Edit: For people who too lazy to check the link, there is the sample code from Facebook.
// Create an object
NSMutableDictionary<FBOpenGraphObject> *restaurant = [FBGraphObject openGraphObjectForPost];
// specify that this Open Graph object will be posted to Facebook
restaurant.provisionedForPost = YES;
// Add the standard object properties
restaurant[#"og"] = #{ #"title":#"Restaurant Name", #"type":#"restaurant.restaurant", #"description":#"a description", #"image":image };
// Add the properties restaurant inherits from place
restaurant[#"place"] = #{ #"location" : #{ #"longitude": #"-58.381667", #"latitude":#"-34.603333"} };
// Add the properties particular to the type restaurant.restaurant
restaurant[#"restaurant"] = #{#"category": #[#"Mexican"],
#"contact_info": #{#"street_address": #"123 Some st",
#"locality": #"Menlo Park",
#"region": #"CA",
#"phone_number": #"555-555-555",
#"website": #"http://www.example.com"}};
// Make the Graph API request to post the object
FBRequest *request = [FBRequest requestForPostWithGraphPath:#"me/objects/restaurant.restaurant"
graphObject:#{#"object":restaurant}];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Sucess! Include your code to handle the results here
NSLog(#"result: %#", result);
_objectID = [result objectForKey:#"id"];
alertTitle = #"Object successfully created";
alertText = [NSString stringWithFormat:#"An object with id %# has been created", _objectID];
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
Checkins have been deprecated in favor of attaching place information to posts, or tagging places in Open Graph stories.
You can refer here
https://developers.facebook.com/docs/graph-api/reference/user/checkins/
Related
I'm implementing deep linking in an app.
Followed every tutorial I found.
Tested every possible setting.
And the result is: If I post a story with "user_generated": "true", Facebook posts the big photo as it should, but there is no link like "view on Instagram", "view on Foursqare" and so on. So people can't reach the actual object.
Facebook settings:
Display Name, Namespace, App Domains, Site URL, Bundle ID, iPhone Store ID, Deep Linking (ON), Native or desktop app?(ON), Is your App Secret embedded?(OFF)
App Restrictions - No restrictions
I'v set up the open graph action and object and they are working without "user_generated": "true".
I'm sending my code (projectnamespace is replaced for this example):
-(void) shareOnFacebook: (NSString *) image {
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
object[#"type"] = #"projectnamespace:photo";
object[#"title"] = _postText.text;
object[#"url"] = responseJson[#"share"];
object[#"og:url"] = responseJson[#"share"];
object[#"al:web"] = responseJson[#"share"];
object[#"web"] = responseJson[#"share"];
if (image) {
NSLog(#"Photo Link: %#", image);
object[#"image"] = #[#{#"url": image, #"user_generated" : #"true" }];
}
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *objectId = [result objectForKey:#"id"];
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"photo"];
[action setObject: #"true" forKey: #"fb:explicitly_shared"];
if (image)
[action setImage:#[#{#"url": image, #"user_generated" : #"true" }]];
// create action referencing user owned object
[FBRequestConnection startForPostWithGraphPath:#"me/projectnamespace:add" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *actionId = [result objectForKey:#"id"];
NSLog(#"Posted story to facebook: %#", actionId);
[self shareAndDismiss: nil];
} else {
// An error occurred
NSLog(#"Encountered an error posting action to Open Graph: %#", error);
[self shareAndDismiss: nil];
}
}];
} else {
// An error occurred
NSLog(#"Error posting the Open Graph object to the Object API: %#", error);
[self shareAndDismiss: nil];
}
}];
}
As far as I have seen, there is no such "View on Foursquare" link on Foursquare posts being shared on Facebook.
The only "View on..." links that I am aware of are for Instagram. However Instagram is a Facebook company. The Facebook and Instagram teams work together for their own photo-sharing experience, which means they are not limited by the API as we are.
I'm afraid that "View on Instagram" is a special feature given to the Instagram app only and that the Facebook API doesn't give that ability to others apps.
I am using iOS sdk v3.18.1, I want to get all my Facebook friends.I can get the friends count but, data is nil.
Here is my code
[FBRequestConnection startWithGraphPath:#"me/friends" parameters:nil HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"result %#",result);
}];
Out put
{
data = (
);
summary = {
"total_count" = 840;
};
}
https://developers.facebook.com/docs/apps/changelog
Since v2.0 you cannot get the full friend list anymore, you only get the friends who authorized your App too.
See my answer in this thread too: how to get a list of all user friends (not only who use the app)?
// declare an array in header file which will hold the list of all friends -
NSMutableArray * m_allFriends;
// alloc and initialize the array only once
m_allFriends = [[NSMutableArray alloc] init];
With FB SDK 3.0 and API Version above 2.0 you need to call below function (graph api with me/friends)to get list of FB Friends which uses the same app.
// get friends which use the app
-(void) getMineFriends
{
[FBRequestConnection startWithGraphPath:#"me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"me/friends result=%#",result);
NSLog(#"me/friends error = %#", error.description);
NSArray *friendList = [result objectForKey:#"data"];
[m_allFriends addObjectsFromArray: friendList];
}];
}
Note : 1) The default limit for the number of friends returned by above query is 25. 2)If the next link comes in result, that means there are some more friends which you will be fetching in next query and so on. 3)Alternatively you can change the limit (reduce the limit, exceed the limit from 25) and pass that in param.
////////////////////////////////////////////////////////////////////////
For non app friends -
// m_invitableFriends - global array which will hold the list of invitable friends
Also to get non app friends you need to use (/me/invitable_friends) as below -
- (void) getAllInvitableFriends
{
NSMutableArray *tempFriendsList = [[NSMutableArray alloc] init];
NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:#"100", #"limit", nil];
[self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}
- (void) getAllInvitableFriendsFromFB:(NSDictionary*)parameters
addInList:(NSMutableArray *)tempFriendsList
{
[FBRequestConnection startWithGraphPath:#"/me/invitable_friends"
parameters:parameters
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"error=%#",error);
NSLog(#"result=%#",result);
NSArray *friendArray = [result objectForKey:#"data"];
[tempFriendsList addObjectsFromArray:friendArray];
NSDictionary *paging = [result objectForKey:#"paging"];
NSString *next = nil;
next = [paging objectForKey:#"next"];
if(next != nil)
{
NSDictionary *cursor = [paging objectForKey:#"cursors"];
NSString *after = [cursor objectForKey:#"after"];
//NSString *before = [cursor objectForKey:#"before"];
NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:
#"100", #"limit", after, #"after"
, nil
];
[self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}
else
{
[self replaceGlobalListWithRecentData:tempFriendsList];
}
}];
}
- (void) replaceGlobalListWithRecentData:(NSMutableArray *)tempFriendsList
{
// replace global from received list
[m_invitableFriends removeAllObjects];
[m_invitableFriends addObjectsFromArray:tempFriendsList];
//NSLog(#"friendsList = %d", [m_invitableFriends count]);
[tempFriendsList release];
}
For Inviting non app friend -
you will get invite tokens with the list of friends returned by me/invitable_friends graph api. You can use these invite tokens with FBWebDialogs to send invite to friends as below
- (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
userInviteTokens, #"to",
nil, #"object_id",
#"send", #"action_type",
actionLinksStr, #"actions",
nil];
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:#"Hi friend, I am playing game. Come and play this awesome game with me."
title:nil
parameters:params
handler:^(
FBWebDialogResult result,
NSURL *url,
NSError *error)
{
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending request : %#", error.description);
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
// User clicked the "x" icon
NSLog(#"User canceled request.");
NSLog(#"Friend post dialog not complete, error: %#", error.description);
}
else
{
NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];
if (![resultParams valueForKey:#"request"])
{
// User clicked the Cancel button
NSLog(#"User canceled request.");
}
else
{
NSString *requestID = [resultParams valueForKey:#"request"];
// here you will get the fb id of the friend you invited,
// you can use this id to reward the sender when receiver accepts the request
NSLog(#"Feed post ID: %#", requestID);
NSLog(#"Friend post dialog complete: %#", url);
}
}
}
}];
}
Since, V2.0 of the Graph API, You will only be able to get the list of the friends who are connected with your app. In v2.0 of the Graph API, calling /me/friends returns the person's friends who use the app. Yes it is possible to get the count but accessing the friend list is not possible.
All the release of Facebook SDK after the month of April denies this functionality of getting the whole list of friends.
REFER : SO QUESTION:Facebook graph API returns empty .... FACEBOOK USER GUIDE
This has been confirmed by FACEBOOK.
I am trying (failing alot!) to use the Facebook iOS sdk. I want to publish a story about the user running without leaving the app. I am trying to use the Facebook built in object "course" and the built in action for a run.
I find the documentation very confusing, my code has become very tangled and I'm sure its the worst way possible of trying to implement this solution.
The error I'm getting with the following code is:
2014-04-01 23:10:13.238 Fitness_App[2313:60b] Encountered an error posting to Open Graph: Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x16ba5190 {com.facebook.sdk:HTTPStatusCode=500, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 1611072;
message = "The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: course.";
type = Exception;
};
};
code = 500;
}, com.facebook.sdk:ErrorSessionKey=}
I have been struggling with this and could not get a solution!
-(void) publishStory
{
// instantiate a Facebook Open Graph object
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
// specify that this Open Graph object will be posted to Facebook
object.provisionedForPost = YES;
// for og:title
object[#"title"] = #"running title";
// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object[#"type"] = #"fitness.course";
// for og:description
object[#"description"] = #"running description";
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
// get the object ID for the Open Graph object that is now stored in the Object API
NSString *objectId = [result objectForKey:#"id"];
NSLog([NSString stringWithFormat:#"object id: %#", objectId]);
// Further code to post the OG story goes here
// create an Open Graph action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"fitness.course"];
[FBRequestConnection startForPostWithGraphPath:#"/me/fitness.runs" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog([NSString stringWithFormat:#"OG story posted, story id: %#", [result objectForKey:#"id"]]);
} else {
// An error occurred, we need to handle the error
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred
NSLog(#"Error posting the Open Graph object to the Object API: %#", error);
}
}];
}
Two places where things went wrong.
object[#"type"] = #"fitness.course";
The type should equal #"namespace:object".
[action setObject:objectId forKey:#"fitness.course"];
The key is your object name.
Check your code again and have fun ^-^
Try replacing your this sentence
"[action setObject:objectId forKey:#"fitness.course"];"
with this one
"[action setObject:objectId forKey:#"course"];"
I'm developing a simple app that has a feature to compose user pictures and post them as a OG Story to facebook.
I followed the Facebook docs and I'm issuing the POST requests (I know I could batch them, I'm just trying to get any version working), I do get an story ID, but I can't find the story on Facebook. (permission set to friends)
I'm posting it with the path /me/:
When I check in the Graph Explorer this path returns an empty { 'data' : [] }
I know the story won't be posted on my profile, but I need to see it somewhere.
I need to test things such as deep linking (I know url is currently set to nil), but I can't find the story! The only place I could manage to see the picture is when I go to
developer page and click on 'preview'.
Any ideas?
Thanks in advance.
Here is the source code:
- (void) publishPhoto
{
// stage an image
[FBRequestConnection startForUploadStagingResourceWithImage:self.photo.image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(#"Successfuly staged image with staged URI: %#", [result objectForKey:#"uri"]);
// instantiate a Facebook Open Graph object
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPostWithType:#"<APPNAMESPACE>:picture" title:#"" image:#[#{#"url":[result objectForKey:#"uri"], #"user_generated" : #"true"}] url:nil description:#""];
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
// get the object ID for the Open Graph object that is now stored in the Object API
NSString *objectId = [result objectForKey:#"id"];
NSLog([NSString stringWithFormat:#"object id: %#", objectId]);
// create an Open Graph action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"picture"];
// create action referencing user owned object
[FBRequestConnection startForPostWithGraphPath:#"/me/<APPNAMESPACE>:take" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog([NSString stringWithFormat:#"OG story posted, story id: %#", [result objectForKey:#"id"]]);
[[[UIAlertView alloc] initWithTitle:#"OG story posted"
message:#"Check your Facebook profile or activity log to see the story."
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error staging an image: %#", error.description);
}
}];
}
Try it with iPhone device instead of simulator. It'll work
Some form of this has been asked/answered before but I'm still pretty hazy on the issue. I'm trying to post to a friends feed but keep getting "error com.facebook.sdk code = 5" errors when trying to use startWithGraphPath: from the new FB SDK for ios. The FBSession is active and open and the access_token appears to be correct... Here's some code:
-(void)inviteUser:(NSString *)whoever {
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, so ask for it
[FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:#"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
if (!error){
[self sendInvite:whoever];
}
}
}];
}
-(void) sendInvite:(NSString *)whoever {
NSMutableDictionary *params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"A name of something", #"name",
nil];
[FBRequestConnection
startWithGraphPath:[NSString stringWithFormat:#"%#/feed", whoever]
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = #"Posted successfully.";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}];
I'm still new at this, and am probably missing something basic. But I'm just not seeing it.
Fixed it. I think there were two problems:
Not having the session properly communicated inside the app (i.e. I had the FBSession open in a loginController, but not in the sendInvite controller <- not the exact names, obviously). As a result, the access_token actually wasn't active. I should have followed the FB docs and put the FBSession methods in the appdelegate.
I was using "publish_action" permissions when I believe I should have been using "publish_stream."
Works smoothly with these two changes. I do have a follow-up question, though: how to post on someone else's wall using the new SDK's native share dialog? I'll probably ask this as a separate question.