Facebook Graph Api Fan page photo upload (IOS)

泄露秘密 提交于 2019-12-25 00:34:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24005368/facebook-graph-api-fan-page-photo-upload-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!