FB Graph API - Create event on behalf of page -> error 403 - ios

I have been trying to create an event on the behalf of my page using the Facebook Graph API for iOS.
However, I always fail with the error code 403. I have tried to create an event on behalf of me - and surprisingly that works. The permissions are set appropriately as stated here.
My code goes like:
NSMutableDictionary *postParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"Test",#"name",
#"2012-11-11", #"start_time", nil];
[FBRequestConnection
startWithGraphPath:#"12345/events"
parameters:postParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", result);
} else {
NSLog(#"Success: %#", result);
}
}];
How I said, if I set the ID/events to my user ID instead of my Page's ID, it works. Could I have set up something badly on my page? Do I need admin rights for that page?
I found something about access tokens to "Perform the following operations as a Page" here. Do I need that? If so, how do I do it?
Thanks to everyone in advance!

To authenticate as a Page (and from there, edit the events) see this document: https://developers.facebook.com/docs/authentication/pages/
In short, authenticate as user who is an an admin of the Page. Then:
https://graph.facebook.com/me/accounts?access_token=xxx
in order to retrieve the access token for the Page (and indeed for any other Pages the user administers).
Then use that access token to administer the events for the Page.

Related

Parameter for like count with facebook api V2.3

I am using this line of code for getting count of likes on facebook post
NSString *postId = #"1234567890_1234567890";
NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:postId,#"ObjectId",nil];
NSString * str = (NSString *) [[[FBSDKGraphRequest alloc] initWithGraphPath:#"/{object-id}/likes"
parameters:photosParams
HTTPMethod:#"GET"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if ([error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] isEqual:#200]) {
NSLog(#"%#",[error localizedDescription]);
}
}];
but I am getting error of unsupported url, any one have Idea what parameter I use to get result. I am using updated version of facebook api
As
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/likes
states, you need
The same permissions required to view the parent object are required to view likes on that object.
That means that the permission setting for Posts (https://developers.facebook.com/docs/graph-api/reference/v2.3/post) apply:
For page posts, public posts by the page are retrievable with any valid access token. Posts by people on the page, posts by people which mention the page, or targeted page posts (by language or geography, for example) may require a user or page token.
A user access token with read_stream or user_posts permission for any other posts
Are you sure you're adding an Access Token to your request?
You can request multiple posts at once like this:
/?ids={object_id1},{object_id2}&fields=id,created_time,likes.summary(true).limit(0)

Fetch Facebook Posts for Public Page using Facebook SDK

I'm trying to fetch Facebook posts for a public page using the Facebook SDK for iOS. I can't seem to figure out how to get and then use the access token I need to make this request. Let's say it's the New York Times page. This shouldn't require any permissions, so I should be able to use the App Access Token...but again, I can't figure out how to get & use that.
The code I am using looks like this:
[FBRequestConnection startWithGraphPath:#"/nytimes/posts" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"DATA: %#", [result data]);
NSLog(#"ERROR: %#", error);
}];
I keep receiving an error saying that an access token is required.
Please help!
It means it asking you a permission to access the public post in view did load method put one line code like (readPermissions = #[#"public_profile", #"email",#"user_friends",#"user_likes",#"user_groups"];)
, for more details see this https://developers.facebook.com/docs/facebook-login/permissions/v2.2.

Facebook iOS SDK posting as a Page

In my iOS app, I want to post on a Facebook Page as the Facebook Page.
I retrieve the page access tokens through the 'me/accounts' endpoint through the user. However, I am not sure how to create an FBSession using that page access token in order to create the post request.
The page access token is a string, and I obtained it correctly by asking the user for 'manage_pages' and 'publish_streams' permissions.
Is there an easy way to do this? I have been looking at the fetchFBAccessTokenData but am not sure how I could use it.
The answer is - and this took me a long time to find - initWithSession:FBSession.activeSession should be initWithSession:nil, and the access token should be set direction in the parameters NSDictionary field as [parameters setObject:yourAccessToken forKey:#"access_token"]. The access token in the FBSession.activeSession overrides the access_token in the parameters (which it shouldn't IMHO), making the post appear as a "fan" post, instead of a post as the page.
[d addObject:yourFetchedAccessTokenString forKey:#"access_token"];
FBRequest *request = [[FBRequest alloc] initWithSession:nil
graphPath:graphPath
parameters:d
HTTPMethod:#"POST"];
Of course now there are other issues, such as how long the access token will last, which requires token caching stategies: https://developers.facebook.com/docs/howtos/token-caching-ios-sdk/
Found answer here: https://github.com/facebook/facebook-ios-sdk/pull/483
Thousand thanks for James O'Brien's Code. In addition to that,
you may need the below code:
FBRequestConnection *con = [[FBRequestConnection alloc] init];
[con addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error){
NSLog(#"facebook result >> %#", result);
[self cancelfbshare:nil];
}];
[con start];

Facebook graph api and nested query

I am trying to get Friendlist details using Facebook iOS SDK using the below query:
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startWithGraphPath:#"me?fields=id,name,friendlists&fields=id,name,members&fields=pic,can_post,id,link,name,pic_crop,pic_large,pic_small,pic_square,profile_type,username,picture,list_type" completionHandler:^(FBRequestConnection *connection,id user, NSError *error) {
}];
I know the mistake is in using the '&' while constructing the query but I am not knowing how to nest the query (to get details like pic, can_post, etc).
Can someone help?
You should do this
me?fields=name,about,accounts.fields(about)
if you'd like to request 'user' (include name, about) and account connection (include 'about' field)
2nd connection you must user . (dot notation) instead of ?fields=
You should use + startForMyFriendsWithCompletionHandler:(FBRequestHandler)handler:
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection,id user, NSError *error) {
}];
}
Note that it would get all the fields from your friends. If you want only a few ones, you can use startWithGraphPath:completion: with #"me/friends?fields=id,name,pic_square as graph path.

Facebook request does not return email

No email is outputted from the Facebook request. How do I get this? I need it for my login/signup process
- (void)request:(FBRequest *)request didLoad:(id)result{
NSLog(#"FACEBOOK RESULT %# ", result);
}
This is the initial request:
[facebook requestWithGraphPath:#"me" andDelegate:self];
The email property cannot be obtained without requesting additional permission to obtain it, see https://developers.facebook.com/docs/reference/login/email-permissions/.
My application uses the following code to first check whether the Facebook session is valid (self.facebook is a Facebook object initialized with initWithAppId):
if (![self.facebook isSessionValid]) {
NSArray* permissions = [NSArray arrayWithObjects: #"email", nil];
[self.facebook authorize:permissions];
} else {
// Since we have a valid session we can get the userinfo
[self getFacebookUserInfo];
}
Your application prompt will indicate "Using this app requires: * Your basic info * Your e-mail address" If the user authorizes you to obtain this information your access token returned will have bits in set to allow you to obtain the e-mail address, and the following will work:
-(void)getFacebookUserInfo {
[self.facebook requestWithGraphPath:#"me" andDelegate:self];
}
assuming your -(void)request:(FBRequest *)request didLoad:(id)result method is available (which it appears that it is).
Note that the full flow of SSO (Single Sign On) is not given here in this post, you'll need to go through https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/#implementsso in detail.
Note also that you may be using a deprecated API for iOS 6, you should look at https://developers.facebook.com/docs/tutorial/iossdk/upgrading-from-2.0-to-3.1/ before going further.
With 2.4 api, parameters are used in order to specify which user data items you are interested in receiving.
E.g.:
FBSDKGraphRequestConnection *connection2 = [[FBSDKGraphRequestConnection alloc] init];
FBSDKGraphRequest *selfRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me?"
parameters:#{#"fields":#"id,name,first_name,last_name,verified,email"}];
[connection2 addRequest:selfRequest
completionHandler:^(FBSDKGraphRequestConnection *innerConnection, NSDictionary *result, NSError *error) {
if (error) {
NSLog(#"%#", error);
return;
}
if (result) {
// get user data
NSMutableDictionary *facebookUserData = [NSMutableDictionary dictionaryWithDictionary:result];
NSLog(#"result=%#", result);
HOWEVER, they apparently messed up big time, as email is NOT CURRENTLY being returned for DEVELOPERS, and currently in development Apps.
Incidentally, it is IMPOSSIBLE to submit bugs on Apps in development, as they require approved apps in the feedback system...
I wonder how do they think we are going to make sure that something works BEFORE submitting an app for approval, or what is the logic of asking for an App Store id of an App which can't be approved BEFORE Facebook integration can be tested to the fullest.
Besides, who is the genius at Facebook who decides to break working code one day from another?

Resources