0

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.

Sean Villani
  • 1,342
  • 1
  • 8
  • 9
  • 1
    Are you using a Page Access Token, or the admin User's Access Token? – Tobi Jun 03 '14 at 06:51
  • I have an NSUserDefaults that's a NSDictionary of a key value pair. The key is the facebook fan page id and the value is the access_token that is returned from the me/accounts graph call. Currently i'm just passing it as a param in the #fanPageId#/photos call which i'm thinking is the problem. But i'm not sure how to send the access token any other way. Is there anyway to update the SLRequest to send the access token? – Sean Villani Jun 04 '14 at 13:04

1 Answers1

0

So it turns out that i was doing everything correctly, the problem was that if i left in:

[request setAccount:[accounts lastObject]];

This causes it to post on my behalf and wouldn't show up in the timeline. removing this code remedied the issue and all my posts are now showing up on the fan pages timeline from the fan page itself and not from my facebook user account.

Thanks Tobi, i kinda knew it was the access token and i thought i was passing it correctly, apparently i wasn't.

So just to re-iterate, don't use setAccount when posting to a fan page as it will overwrite the access_token with your user token and not the fan page token.

Sean Villani
  • 1,342
  • 1
  • 8
  • 9
  • In case it is useful for anyone else - the code in this example gave an Invalid Permission error when I tried it - I had to change the permissions requested to just [@"publish_actions", @"manage_pages"] – Simon East Aug 05 '14 at 21:05