schedule post on facebook page using facebooksdk - ios

I have written following code to post on facebook page but getting this error message: “(#200) Unpublished posts must be posted to a page as the page itself.“;
NSString *accesstoken = #"EAACEdEose0cBAAe49xNZBS5MqswXkRptXgLDRuZBZBPZCZCVl5rxW6Bt0jthaHXMACNTiUWePg9XkVTpttDNsLyWYBaEkQaUCm5vbSJQh8zGwkegAcQRjRggAJajunmMyg0jVIKZAZBzMjbZCKWUv1Tie1UzF8gJCpIxACIVgqx7SqKdPZBnIUaElk3meB7SYo6AZD";
NSString *pageId = [dic valueForKey:#"id"];
NSDictionary *dicPara=#{#"message": #"Hello",#"access_token":accesstoken,#"scheduled_publish_time": [NSNumber numberWithInt:timestamp],#"published": #"false"};
NSLog(#"%#",dicPara);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/feed",pageId]
parameters:dicPara
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
if (error)
{
NSLog(#"%#",error);
}
else
{
[arrPostId addObject:[result valueForKey:#"id"]];
}
}];
Now I have searched for this error and found that you have to set accesstoken of the page instead of user's token. So how can I change user's access token to page accesstoken?

Related

How to use facebook pagination in ios

Hi I have recently started coding for iOS.I am trying to get videos from facebook using the following code.
-(void)facebookGetVideos:(UIViewController*)vc completionHandler:(void (^)(NSMutableArray*))completionBlock{
__block NSMutableArray * itemArray;
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/videos/uploaded"
parameters:#{#"fields":#"source,description,thumbnail,title"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if(error!=nil){
NSLog(#"Error after fb dwnld %#",error);
[MBProgressHUD hideHUDForView:vc.view animated:true];
[self showAlert:#"Error" message:error.localizedDescription viewController:vc];
}else{
NSLog(#"Results %#",result);
itemArray = [[NSMutableArray alloc]init];
// [itemArray addObject:[result valueForKey:#"data"]];
itemArray =[result valueForKey:#"data"];
completionBlock(itemArray);
}
}];
}
The response I am getting from there is as below:-
{
data = (
{
id = 1845903302379295;
source = "https://video.xx.fbcdn.net/v/t43.1792-2/27475816_220074475216744_8742438766032977920_n.mp4?efg=eyJybHIiOjE1MDAsInJsYSI6MTAyNCwidmVuY29kZV90YWciOiJzdmVfaGQifQ%3D%3D&rl=1500&vabr=382&oh=d4c3f6028a979c5919fdd8e444e24179&oe=5A89882A";
},
{
id = 1814936632142629;
source = "https://video.xx.fbcdn.net/v/t42.14539-2/23925286_2650490725061654_2502749796398268416_n.mp4?efg=eyJybHIiOjU3NiwicmxhIjo1MTIsInZlbmNvZGVfdGFnIjoic2QifQ%3D%3D&rl=576&vabr=320&oh=2fde1aea6eff33d8139ccb8fe7a33fd4&oe=5A8980C0";
}
);
paging = {
cursors = {
after = MTgxNDkzNjYzMjE0MjYyOQZDZD;
before = MTg0NTkwMzMwMjM3OTI5NQZDZD;
};
};
}
I am trying to use this after before as parameters as below:-
-(void)afterValues:(NSString*)afterVal{
NSDictionary * parameters =#{#"fields":#"source",#"after": afterVal};
FBSDKGraphRequest *request1 = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/videos/uploaded"
parameters:parameters
HTTPMethod:#"GET"];
[request1 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSLog(#"after videos%#",result);
}];
}
And getting the following as response:-
after videos{
data = (
);
}
Can anyone please point out at my mistake.Any kind of help/suggestion or guidance in this directions would be highly appreciated.Thanks in advance!
Have you requested the manage_pages permission from the respective user through the login dialog? I think you see an empty result because of the missing permission.

Post a photo on a Facebook Public page

I'm trying to post a photo from an iOS app to a public page.
I log in the app as the Admin of the FB App and when I got the activity token through Graph API:
FBSDKGraphRequest *request2 = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/accounts"
parameters: #{#"access_token": item.token}
HTTPMethod:#"GET"];
[request2 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
{
NSDictionary *userData = (NSDictionary *)result;
NSArray* arraydata = [userData objectForKey:#"data"];
NSDictionary *dataD = [arraydata objectAtIndex:0];
NSString *facebookID = dataD[#"id"];
token= dataD[#"access_token"];
}
I get the page token that I need to post on the page, but even if in the json I see I have all the permissions, when I post the photo it returns a 200 error (additional permission required).
If I try to post on the Admin profile (not the public page), everything works.
Any idea? What I'm missing? Can anyone you please solve this issue?

post google static map to facebook wall

I want to post a google static map to facebook wall
this is map url
and open by chrome, is fine
but it wrong on facebook, I have three marker in this map
but just last one stay,as below
(the last marker is test if facebook just keep last one)
here is my code
NSString *staticMap = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/staticmap?size=600x315&maptype=roadmap&markers=color:blue|label:S|%f,%f&markers=color:green|label:E|%f,%f&markers=color:red|label:T|25.2,122.5",dataManager.postCarpoolStartLat,dataManager.postCarpoolStartLng,dataManager.postCarpoolEndLat,dataManager.postCarpoolEndLng];
NSURL *staticMapURL = [[NSURL alloc]initWithString:[staticMap stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"static url : %#",staticMapURL);
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"])
{
NSDictionary *params = #{
#"message":#"this is test",
#"caption":#"this is caption",
#"description":#"description",
#"link":#"www.google.com",
#"name":#"Name",
#"picture":staticMapURL,
#"type":#"mobile",
};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"post"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error){
if (!error)
{
NSLog(#"post success");
}
}];
}
Is anyone know how to let facebook show the right map?
thanks in advance

Sorting Through Graph API of Facebook Page on iOS

I am using:
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
This gives me a JSON string of ALL data (AND I MEAN ALL) from a Facebook Page. It gives me the posts, IDs of those who liked the posts, every comment, every person who shared. I really just need the post itself, which is listed as 'message' in the JSON result. Is there a way I can do this in the API call, or does it have to be done after?
Also,
Is there any way to get it to pull the pictures that are associated with each post? I know how to get photos posted to page, but I just want to view the posts made to the page, and have it also pull up the picture.
You can filter out feed response as below
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/v2.3/ID/feed" parameters:[NSMutableDictionary dictionaryWithObject:#"id, message, link.picture" forKey:#"fields"]]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
As you can check, I've mentioned the parameters which I information I requires to get filter out.
NOTE: You can filter out things according to your requirement.
Please check below link for available filter possibilities on facebook SDK.
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
I hope it will help you, Not sure about the picture you want to pull up but may be 'link.picture' in 'fields' will help you the picture you want to get.
Use
/{page_id}/feed?fields=id,message
With Graph API v2.4 this will be the standard usage that you have to specify each field.
this, it's the variable of NSString "link" that you want:
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *link = userData[#"link"];
NSString *locale = userData[#"locale"];
NSString *timezone = userData[#"timezone"];
NSString *last_name = userData[#"last_name"];
NSString *email = userData[#"email"];
NSString *gender = userData[#"gender"];
NSString *first_name = userData[#"first_name"];
} else if (error) {
//tbd
}
}];
} else if (![FBSDKAccessToken currentAccessToken]) {
//tbd
}

ios facebook sdk get and display username

I have been working with the facebook sdk and parse and I am trying to retrieve a users facebook name but the documentation doesn't give me sufficient help and most tutorials on the net are out dated.
Can anyone tell how I can get a users name from the facebook sdk to display as a text label ? This would be greatly appreciated thanks.
You need to implement following FBLoginViewDelegate
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
NSLog(#"user is %# %#", [user objectForKey:#"email"], user.name);
}
You can retrieve it in this way:
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me"];
[request startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary < FBGraphUser > *my,
NSError *error)
{
NSString *username = my[#"username"];
}
I finally figured it out, you have to request the current users profile information than set your label with a "key" which in this case is called "name"
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me"
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"fetched user:%#", result);
_usernameText.text = result[#"name"];
}];

Resources