我在将ios应用中的照片发布到Facebook页面作为页面管理员时遇到问题。我作为管理员发布的唯一内容是状态消息。
这就是我的所作所为。
// 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);
}];
}
}];
以上代码成功发布到Facebook页面作为管理员。由于我需要发布照片,我需要将grap路径更改为/ 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);
}];
以上代码已成功发布,但不是管理员。虽然我正在使用页面的访问令牌。
我认为路径fb_page_id /照片已损坏,所以我尝试使用fb_page_id / feed并发送其他字段,例如链接,图片等。但是,它仍然以用户身份发布,而不是网页的管理员。
如果我做错了,请告诉我。
一些信息: - 部署目标:5.1及以上 - Facebook SDK版本:3.7
提前谢谢。
答案 0 :(得分:1)
我终于找到了答案。以防有人遇到同样的问题,这就是我正在做的事情。
访问/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"];
}
}];
获取Page的access_token后,您需要使用REST API发布图像。我在这里使用AFNetworking,并手动上传图像。
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];
}
不要问我为什么使用REST API,我尝试使用他们的SDK,但它总是失败。
我希望有人会发现它对这段代码很有用。