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];
Related
I tried to post image in facebook fan page.
I am using below code:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/facebookPage_ID/photos"
parameters:#{ #"url": #"https://dncache-mauganscorp.netdna-ssl.com/thumbseg/378/378006-bigthumbnail.jpg",}
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
}];
Current image posts to facebook page successfully but image "did not post in facebook page wall" is shown in visitor post.
I want to show image in facebook page wall.
You need to use a Page Access Token instead of a User Access Token:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/
The steps:
Get a User Token with the manage_pages and publish_pages permissions
Use /page-id?fields=access_token to get a Page Token for a specific Page
I have two queries;
1- How can i get user's tagged videos as the below request to Graph API returns me nil on request :( and i have spend about thousands of minutes on searching for this issue but did not find the accurate answer or reason of this issue.
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/videos/tagged"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
NSLog(#"Result is:%#",result);
}];
2- How to make a call for user's saved videos on Facebook to Graph API?
I am Running FB SDK Version 4.20.2 and Graph API version 2.9.
Currently I am using a test account for this purpose and all videos are public.
This is what I'm using now:
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me?fields=id,name,friends{relationship_status}"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSLog(#"Result: %#", result);
}];
}
but the result contains an empty list of friends. I guess this list contains the friends that have installed this same app.
Using the Graph Tool Explorer I can get this list
GET /v2.3/me?fields=id,name,friends{relationship_status,gender}
How can I get this list in iOS?
Permissions that I'm calling are:
loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
***** UPDATE *****
Ok, I've read the confirmation: "Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app."
Has anyone used taggable_friends or something like that to get the list of friends making more requests later to get the information about each and every friend? Is this something Facebook can reject when submitting the app?
That's not possible, because all friends_* permissions have been removed. Please don't waste your time trying to find a workaround.
See
https://developers.facebook.com/docs/apps/changelog#v2_0
All friends_* permissions have been removed.
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)
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.