我需要从我的应用程序将 Open Graph 故事发布到 Facebook 墙上。我遵循了 Facebook 开发人员的最新教程。
我在 Facebook 仪表板上创建了对象、动作和故事。然后在我的应用程序中执行以下代码:
创建一个对象:
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
object[@"title"] = @"test title";
object[@"type"] = @"myapp:my_object_type";
object[@"description"] = @"test discription";
object[@"url"] = @"http://example.com";
object[@"image"] = @[@{@"url": [result objectForKey:@"uri"], @"user_generated" : @"false" }];
并发布:
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSString *objectId = [result objectForKey:@"id"];
NSLog(@"object id %@",objectId);
} else {
NSLog(@"Error posting the Open Graph object to the Object API: %@", error);
}
}];
} else {
NSLog(@"Error staging an image: %@", error);
}
}];
结果得到了对象ID。
然后创建一个动作并将其与对象链接:
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:@"my_object_type"];
FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
params.action = action;
params.actionType = @"myapp:my_app_action";
并将其发布到 Facebook:
[FBRequestConnection startForPostWithGraphPath:@"/me/myapp:my_app_action" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(@"OG story posted, story id: %@", [result objectForKey:@"id"]);
[[[UIAlertView alloc] initWithTitle:@"OG story posted"
message:@"Check your Facebook profile or activity log to see the story."
delegate:self
cancelButtonTitle:@"OK!"
otherButtonTitles:nil] show];
} else {
NSLog(@"Encountered an error posting to Open Graph: %@", error);
}
}];
结果,我收到消息说 OG 故事已成功发布,但是当转到 Facebook 时,提要或时间线中没有任何内容,只是添加到用户相册中的图像。