I have an app that uploaded photos on the fly and i'm trying to add the ability to upload to the users facebook fan page (that they administer).
The problem that i'm having is that i can sucessfully upload a photo to the fan page but it's not showing up on the fan page timeline, it's only showing up under the "Recent Posts by Others" section of the fan page.
I'm obviously not uploading these images properly. Here's my code:
ACAccountType * accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_accountStore requestAccessToAccountsWithType:accountType options:@{ACFacebookAppIdKey: @"XXXXXXXXXXXX", ACFacebookPermissionsKey : @[@"photo_upload",@"publish_stream", @"publish_actions", @"manage_pages"], ACFacebookAudienceKey : ACFacebookAudienceEveryone} completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray * accounts = [_accountStore accountsWithAccountType:accountType];
NSDictionary *fanPages = [[NSUserDefaults standardUserDefaults] objectForKey:FACEBOOK_PREF_FAN_PAGES_IDS];
//if using fan pages
if (fanPages.count)
{
for (id key in fanPages) {
//id is the key and the access token is the value
NSLog(@"access token:%@, fanpage id: %@", fanPages[key], key);
NSMutableString *requestURL = [NSMutableString stringWithFormat:@"https://graph.facebook.com/%@/photos/", key];
NSURL * url = [NSURL URLWithString:requestURL];
NSDictionary *parameters = @{@"access_token": [fanPages[key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @"no_story":@"0", @"message": hashtags};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:parameters];
[request addMultipartData:UIImageJPEGRepresentation(image, 0.9) withName:@"source" type:@"application/octet-stream" filename:@"media.png"];
//NSLog(@"%@",[accounts lastObject]);
[request setAccount:[accounts lastObject]];
//make request
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"uploaded for %@",fanPages[key]);
NSString *respData = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
NSLog(@"responseData %@",respData);
NSLog(@"urlResponse for %@",urlResponse);
[self performSelectorOnMainThread:@selector(removeProgressView) withObject:nil waitUntilDone:NO];
}];
}
}
else
{
//upload to personal timeline
NSURL * url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:nil];
[request addMultipartData:[hashtags dataUsingEncoding:NSUTF8StringEncoding] withName:@"message" type:@"multipart/form-data" filename:nil];
[request addMultipartData:UIImageJPEGRepresentation(image, 0.9) withName:@"source" type:@"application/octet-stream" filename:@"media.png"];
[request setAccount:[accounts lastObject]];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
[self performSelectorOnMainThread:@selector(removeProgressView) withObject:nil waitUntilDone:NO];
}];
}
} else {
//theres an error or they are not granted
}
Any Ideas on what i'm doing wrong? I've scoured the internet for days on this one and everything seems correct.