简体   繁体   中英

ios facebook api can't post graph object with custom properties

I'm following this tutorial..

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

and I'm getting a error message when posting a open graph object with custom properties.

This is the method I use to post the story

- (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);
    }
}];

}

I call the method with this..

     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];

This is the object before sending it.

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";
}

This is the error I get.

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;
    };
}

According to the error I'm not supplying app_namespace:levelname, but as far as I can see, the input is correct. So what is going wrong here? I've tried without the namespace, but that gives the same error message.

According to the documentation custom properties can be added to the graph object like normal properties, so I'm not sure why I'm getting this error.

You should specify the "url" started with "http://", I encountered this issue also, and finally figure this out with the above solution. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM