Gyfis
Gyfis

Reputation: 1174

ObjC, Facebook Page - posting news feed works, but posting photo does not

so I'm trying to post a photo on my Facebook page (I'm admin) from iPhone app. I'm using FB Sessions to create the session, get the read permission, get manage_pages permission, then, I successfully get my Facebook Pages app-ids as a result of

[FBRequestConnection startWithGraphPath:@"/me/accounts" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
 {
     NSLog(@"%@", [result description]);
 }]; // have token, what now?

Which is still fine. Then, I try to post a photo to the app_id/photos feed, and it does not work = it uploads the photo correctly, but shows me (as in my profile) uploading the photo rather then the Page itself. What could be the problem?

Here's the code for the params and the call

NSDictionary *params = [NSDictionary 
dictionaryWithObjects: [NSArray arrayWithObjects:
    [UIImage imageNamed:@"Icon.png"], 
    @"This is my first photo post from xcode app", nil] 
forKeys:[NSArray arrayWithObjects:
    @"source", 
    @"message", nil]];

[FBRequestConnection startWithGraphPath:@"MyPageID/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
 {
     NSLog(@"%@", [result description]);
 } ];

the [result description] log from the call is fine (it returns the id = xx and "post_id" = yy, which I assume are correct), as is the call itself - the only problem is that it shows me as the author, and not the Page itself. MyPageID is correct, because calling

NSDictionary *paramsFeed = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"This is my first page post from xcode app", nil] forKeys:[NSArray arrayWithObjects:@"message", nil]];

[FBRequestConnection startWithGraphPath:@"390106241058188/feed" parameters:paramsFeed HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
 {
     NSLog(@"result: %@", [result description]);
 }];

Also, calling me/photos and posting the photo there with dictionaryKey @"picture" works, and it posts the photo on my own wall perfectly.

Do anyone knows where the problem could be hidden?

Upvotes: 2

Views: 697

Answers (1)

Gyfis
Gyfis

Reputation: 1174

I found the solution, although I find it weird that I did not have to do this for the /feed page update

Huge thanks goes for Mr. Arpit Kumar Kulshrestha, who pointed out the right way in my previous incarnation of the problem ( Xcode - how to share photo from iPhone app to Facebook managed page feed )

the solution: I've tried setting a new FBSession before, but it still had a lot of errors in it, so I've left that route and went to another one. However, it makes perfect sense to do that - create FBAccessTokenData, which has the Page's token in it, and set other things to the same as the active session - like this

FBAccessTokenData *tokenData = [FBAccessTokenData createTokenFromString:pageTokenString permissions:[FBSession activeSession].accessTokenData.permissions expirationDate:[FBSession activeSession].accessTokenData.expirationDate loginType:FBSessionLoginTypeFacebookApplication refreshDate:nil];

then, one need to create a new FBSession, and what is important, it needs to have set its tokenCacheStrategy to something without data, i.e.[FBSessionTokenCachingStrategy nullCacheInstance] . I did a blank allocation ([[FBSession alloc] init]), and that gave me a lot of errors, however when I use this one

FBSession *sessionFb = [[FBSession alloc] initWithAppID:appID permissions:[NSArray arrayWithObjects: @"publish_stream", @"manage_pages", nil] urlSchemeSuffix:nil tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance]];

it works perfectly. And when you have this new session, you want to open the path for the data to come and go, and you can make it like this:

[sessionFb openFromAccessTokenData:tokenData completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
 {

 }];

and the last part before it starts working correctly is setting the activeSession to this newly allocated and opened session, this way:

[FBSession setActiveSession:sessionFb];

after I've made all of these changes, the photo page sharing started to work magically. However, I'm still not sure how is it possible that the photo share did not work before but the feed share did.. but this solves my issue.

Upvotes: 1

Related Questions