简体   繁体   中英

Publishing picture to facebook page as admin

I have problem publishing a photo from ios app to facebook page as the page's admin . The only thing I can publish as admin is a status message.

Here what I did.

// get access token first
[FBRequestConnection startWithGraphPath:@"/__facebook_page_id__?fields=access_token" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

    // make sure its not nil
    if ([result valueForKey:@"access_token"] != nil) {
        NSString *accessToken = [result objectForKey:@"access_token"];

        NSLog(@"Access token: %@", accessToken);

        NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
        [params setObject:@"This is some status message" forKey:@"message"];
        [params setObject:accessToken forKey:@"access_token"];

        [FBRequestConnection startWithGraphPath:@"/__facebook_page_id__/feed" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

            NSLog(@"Result: %@", result);
            NSLog(@"Error: %@", error);

        }];
    }

}];

The above code successfully publish to facebook page as the admin . Since I need to publish a photo, I need to change the grap path to /facebook_page_id/photos.

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:UIImagePNGRepresentation(self.imagePreview.image) forKey:@"picture"];
[params setObject:@"This is some description" forKey:@"message"];
[params setObject:accessToken forKey:@"access_token"];

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/__facebook_page_id__/photos"] parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

    NSLog(@"Result: %@", result);
    NSLog(@"Error: %@", error);

}];

The above code is successfully published, but not as admin. Eventhough I am using the page's access token.

I thought the path fb_page_id/photos was broken, so I tried to use the fb_page_id/feed and send other fields such as link, picture, etc. But still, it posted as the user, not the page's admin.

Please let me know if I am doing wrong.

Some Information: - Deployment target: 5.1 and above - Facebook SDK Version: 3.7

Thank you in advance.

I finally found the answer. Just in case somebody having the same problem, here is what I am doing.

  1. You will need a Page's access_token by accessing /page_id?fields=access_token

    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@?fields=access_token", FB_PAGE_ID] completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

     // if the access_token is not there, the user might not able to post. check the permission if ([result valueForKey:@"access_token"] != nil) { NSString *accessToken = [result objectForKey:@"access_token"]; } 

    }];

  2. After getting the Page's access_token, you need to post the image using REST API. Here I am using AFNetworking, and manually upload the image.

    if ([result valueForKey:@"access_token"] != nil) { NSString *accessToken = [result objectForKey:@"access_token"];

     NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com"]; AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:url]; // parameter NSDictionary *params = @{@"access_token": accessToken, @"message": message}; // image data NSData *imageData = UIImageJPEGRepresentation(self.imagePreview.image, 1); // create Request and upload the image NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:[NSString stringWithFormat:@"/%@/photos/", [defaults stringForKey:@"fb_page_id"]] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"source" fileName:accessToken mimeType:@"image/jpeg"]; }]; // publish! AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request1 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { // IMAGE PUBLISHED!!! } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { // SOMETHING WRONG HAPPEN }]; [json start]; 

    }

Don't ask me why I'm using REST API, I tried everything using their SDK, but its always failed.

I hope somebody will find it usefull for this piece of codes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM