ios facebook api无法使用自定义属性发布图形对象

时间:2014-04-23 16:54:36

标签: ios facebook facebook-graph-api

我遵循本教程..

https://developers.facebook.com/docs/ios/open-graph/

我在发布带有自定义属性的打开图表对象时收到错误消息。

这是我用来发布故事的方法

- (void) postToFacebookWithMessage:(NSString *)message andImageURL:(NSString*) url withObjectType:(NSString*)objectType withActionType:(NSString*) actionType withTitle:(NSString*) title withCustomProperties:(NSDictionary*) dict

{     NSMutableDictionary * object = [FBGraphObject openGraphObjectForPost];

// specify that this Open Graph object will be posted to Facebook
object.provisionedForPost = YES;

// for og:title
object[@"title"] = title;

// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object[@"type"] = [NSString stringWithFormat:@"app_namespace:%@",objectType]; //graphType; //@"fbogsample:dish";

// for og:description
object[@"description"] = message;

// for og:url, we cover how this is used in the "Deep Linking" section below
object[@"url"] = [NSString stringWithFormat:@"someurl/%@",objectType];

if (dict != nil)
{
    [object addEntriesFromDictionary:dict];
}
// for og:image we assign the image that we just staged, using the uri we got as a response
// the image has to be packed in a dictionary like this:

//if there is no url take standard icon.
if (url == nil)
{
    url = @"http/some_image.jpg";
}

object[@"image"] = @[@{@"url": url, @"user_generated" : @"false" }];

[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if(!error) {
        // get the object ID for the Open Graph object that is now stored in the Object API
        NSString *objectId = [result objectForKey:@"id"];
        NSLog([NSString stringWithFormat:@"object id: %@", objectId]);

        // Further code to post the OG story goes here
        // create an Open Graph action
        id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
        [action setObject:objectId forKey:objectType];

        // create action referencing user owned object
        [FBRequestConnection startForPostWithGraphPath:[NSString stringWithFormat:@"/me/muffins_coffins:%@",actionType] graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if(!error) {
                NSLog([NSString stringWithFormat:@"OG story posted, story id: %@", [result objectForKey:@"id"]]);

            } else {
                // An error occurred
                NSLog(@"Encountered an error posting to Open Graph: %@", error);
            }
        }];

    } else {
        // An error occurred
        NSLog(@"Error posting the Open Graph object to the Object API: %@", error);
    }
}];

}

我用这个调用方法..

     NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:opponentName,@"app_namespace:opponentname",
opponentId,@"app_namespace:opponentid", levelName,@"app_namespace:levelname", nil];

     [appDelegate postToFacebookWithMessage:@"some message" andImageURL:nil 
withObjectType:@"match" withActionType:@"won" withTitle:@"Match finished" 
withCustomProperties:dict];

这是发送前的对象。

Printing description of object:
{
    data =     {
    };
    description = "some description";
    "fbsdk:create_object" = 1;
    image =     (
                {
            url = "some_image.jpg";
            "user_generated" = false;
        }
    );
    "app_namespace:levelname" = "level 3";
    "app_namespace:opponentid" = 100007390148688;
    "app_namespace:opponentname" = "Some name";
    title = "Match finished";
    type = "app_namespace:match";
    url = "://www.somewebsite/match";
}

这是我得到的错误。

Printing description of error->_userInfo:
{
    "com.facebook.sdk:ErrorSessionKey" = "<FBSession: 0x1cd5ccd0, state: FBSessionStateOpen, loginHandler: 0x3e4d50, appID: 1436791226553400, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x1cd4af40>, expirationDate: 2014-06-14 12:29:14 +0000, refreshDate: 2014-04-15 12:31:52 +0000, attemptedRefreshDate: 2014-04-23 16:37:01 +0000, permissions:(\n    \"create_note\",\n    \"basic_info\",\n    \"share_item\",\n    \"status_update\",\n    \"user_friends\",\n    \"publish_actions\",\n    \"publish_checkins\",\n    \"video_upload\",\n    \"publish_stream\",\n    \"photo_upload\",\n    installed,\n    \"public_profile\"\n)>";
    "com.facebook.sdk:HTTPStatusCode" = 400;
    "com.facebook.sdk:ParsedJSONResponseKey" =     {
        body =         {
            error =             {
                code = 100;
                message = "(#100) Object Missing a Required Value: Object at URL 'com/match' of type 'app_namespace:match' is invalid because a required property 'app_namespace:levelname' of type 'string' was not provided.";
                type = OAuthException;
            };
        };
        code = 400;
    };
}

根据错误我没有提供app_namespace:levelname,但据我所知,输入是正确的。那么这里出了什么问题?我试过没有命名空间,但是它给出了相同的错误信息。

根据文档,自定义属性可以像普通属性一样添加到图形对象中,因此我不确定为什么我会收到此错误。

1 个答案:

答案 0 :(得分:0)

你应该指定以“http://”开头的“url”,我也遇到过这个问题,最后用上面的解决方案来解决这个问题。祝好运!