Posting photos to Facebook fan page with iOS SDK
Asked Answered
K

5

9

I'm trying to upload a photo to a Facebook page I own using the iOS SDK and graph API. I've been able to upload photos to my own wall. I've also been able to post a message to my fan page wall as an admin and create an album on my fan page, so I think I'm making the right calls, but not succeeding.

This is what I'm currently doing.

I'm logging in using

Facebook *fb = [[Facebook alloc] init];
self.facebook = fb;
[fb release];
NSArray *permissions =  [[NSArray arrayWithObjects:@"read_stream", @"offline_access", @"publish_stream", @"manage_pages", @"user_photos", @"friends_photos",nil] retain];
[facebook authorize:FB_APP_ID permissions:permissions delegate:self];
[permissions release];

After I log in, I request the accounts

[facebook requestWithGraphPath:@"/me/accounts" andDelegate:self];

The accounts are returned to me with access tokens for each page and app I own. I use a simple loop to parse through the accounts to extra the access_token supplied for the page I wish to upload a photo to.

I then set my access token of my facebook object to the new access_token.

facebook.accessToken = new_accessToken;

and try and upload a photo my fan page using

UIImage *uploadImage = [UIImage imageNamed:@"t200.jpg"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
               uploadImage, @"source", 
             @"test caption", @"message",             
             nil];


[facebook requestWithGraphPath:@"/me/photos" andParams:params
        andHttpMethod:@"POST" andDelegate:self];

The response I receive is

{"error":{"type":"OAuthException","message":"(#1) An unknown error occurred"}}

Is this a bug in the facebook graph API? I also tried making this call using standard HTTPS requests, and get the same result.

Koerlin answered 23/11, 2010 at 18:45 Comment(0)
L
4

Ater successfully login to Facebook, you can get list FanPage by requesting @"/me/accounts" and iterate to the fan page you want and request its accessToken by making request to @"/FBFanPageID?fields=access_token"

This accessToken is different from your facebook.accesstoken .

After getting fanPage accessToken, set it as facebook accessToken by

[_facebook setAccessToken:_accessTokenForFanPage]; // this is necc step

Now you can upload to fanPage wall by @"/FBFanPageID/photos"

Request fanPage albums by @"/FBFanPageID/albums"

Upload to particular album by @"/AlbumsID/photos"

For uploading these image param dictionary will be used.

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: IMAGE,@"source", IMAGECAPTION,@"message",
nil];

Lubumbashi answered 29/1, 2012 at 6:21 Comment(0)
T
1

Old news but...not sure about the fan page part but you need to add "?access_token=..." after "/me/photos" or whatever you end up using for the graph API to get past the unknown error. The facebook sdk doesn't automatically add the access token so you have to do it yourself.

[facebook requestWithGraphPath:[NSString stringWithFormat:@"/me/photos?access_token=%@", facebook.accessToken] andParams:params andHttpMethod:@"POST" andDelegate:self];

Tralee answered 14/7, 2011 at 17:28 Comment(1)
If you look in Facebook.m as provided in the SDK, you'll see that the access_token is added to the params by the SDK. However, one needs to get a different access_token for the fan page.Wile
A
1

I have very similar codes and it works, the only thing that is slightly different is I create the Facebook instance in the AppDelegate ..

QRSMAppDelegate *delegate = (QRSMAppDelegate *)[[UIApplication sharedApplication] delegate];

if (![[delegate facebook] isSessionValid]) {
    [[delegate facebook] authorize:permissions];

Like you I initially created the Facebook instance in the view controller and I came out with a lot of weird errors. The issue (I later found out) is ... Facebook responds by passing a URL to the Facebook class instance and from outside the only entry point to your app is the app delegate. (Check the Facebook sdk sample app Hackbook)!

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

BOOL ret = [facebook handleOpenURL:url];

.... return(ret); }

The issue here of course is what if I only use Facebook in a small part of my app and miles away from the app delegate (Facebook instantiation require you to declare the delegate!

Rather than face equally complex issues in passing the url up, it was actually easier to just alloc init the view controller (Facebook delegate) in the app delegate and pass it up the view hierarchy.

Alephnull answered 25/3, 2012 at 6:2 Comment(0)
S
0

Not sure about this since I am quite new to FB iOS integration but I think you shouldn't use me/photos but the xyz/photos where xyz is the ID of the page you want to post to.

Sixtyfourmo answered 30/11, 2010 at 9:52 Comment(1)
I've tried it with the page id but still that doesn't work either. Using me seems correct because when you change the access token, what you accessing. So me accesses your fan page.Koerlin
W
0

You can send an email to your fan page and get the attached photo to show up as if the admin posted it.

On your fan page, choose to edit the page and then choose mobile. On the mobile page Facebook will show you an email address you can use to post to your fan page.

In iOS I use the http://code.google.com/p/skpsmtpmessage/ to send the email

You must use this...

svn checkout http://skpsmtpmessage.googlecode.com/svn/trunk/ skpsmtpmessage-read-only

to download the code

Don't include any text in the email besides something in the subject line. Otherwise it won't post.

Welcome answered 1/6, 2012 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.