Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

objective c - Posting photos to Facebook fan page with iOS SDK

I'm trying to upload a photo to a Facebook page I own using the iOS SDK and graph API. I've been able to upload photos to my own wall. I've also been able to post a message to my fan page wall as an admin and create an album on my fan page, so I think I'm making the right calls, but not succeeding.

This is what I'm currently doing.

I'm logging in using

Facebook *fb = [[Facebook alloc] init];
self.facebook = fb;
[fb release];
NSArray *permissions =  [[NSArray arrayWithObjects:@"read_stream", @"offline_access", @"publish_stream", @"manage_pages", @"user_photos", @"friends_photos",nil] retain];
[facebook authorize:FB_APP_ID permissions:permissions delegate:self];
[permissions release];

After I log in, I request the accounts

[facebook requestWithGraphPath:@"/me/accounts" andDelegate:self];

The accounts are returned to me with access tokens for each page and app I own. I use a simple loop to parse through the accounts to extra the access_token supplied for the page I wish to upload a photo to.

I then set my access token of my facebook object to the new access_token.

facebook.accessToken = new_accessToken;

and try and upload a photo my fan page using

UIImage *uploadImage = [UIImage imageNamed:@"t200.jpg"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
               uploadImage, @"source", 
             @"test caption", @"message",             
             nil];


[facebook requestWithGraphPath:@"/me/photos" andParams:params
        andHttpMethod:@"POST" andDelegate:self];

The response I receive is

{"error":{"type":"OAuthException","message":"(#1) An unknown error occurred"}}

Is this a bug in the facebook graph API? I also tried making this call using standard HTTPS requests, and get the same result.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ater successfully login to Facebook, you can get list FanPage by requesting @"/me/accounts" and iterate to the fan page you want and request its accessToken by making request to @"/FBFanPageID?fields=access_token"

This accessToken is different from your facebook.accesstoken .

After getting fanPage accessToken, set it as facebook accessToken by

[_facebook setAccessToken:_accessTokenForFanPage]; // this is necc step

Now you can upload to fanPage wall by @"/FBFanPageID/photos"

Request fanPage albums by @"/FBFanPageID/albums"

Upload to particular album by @"/AlbumsID/photos"

For uploading these image param dictionary will be used.

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: IMAGE,@"source", IMAGECAPTION,@"message",
nil];


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...