How to publish from iOS application to facebook wall without user amending message

23,121

Solution 1

Solution found. The authentication should be called first. Once authenticated, this will call the fbGraphCallback function which will perform the post the the users stream.

All of this was made possible courtesy of http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api. And bonus points goes to The Mad Gamer. Thanks a lot.

-(IBAction)buttonPublishOnFacebookPressed:(id)sender {
    /*Facebook Application ID*/
    NSString *client_id = @"123456789012345";

    //alloc and initalize our FbGraph instance
    self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];

    //begin the authentication process.....
    [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) 
                         andExtendedPermissions:@"publish_stream" andSuperView:self.view];
}

-(void)fbGraphCallback:(id)sender {

    if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {

        NSLog(@"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");

    } else {
        NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook...  Your oAuth token is:  %@", fbGraph.accessToken);

        NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];

        [variables setObject:@"http://farm6.static.flickr.com/5015/5570946750_a486e741.jpg" forKey:@"link"];
        [variables setObject:@"http://farm6.static.flickr.com/5015/5570946750_a486e741.jpg" forKey:@"picture"];
        [variables setObject:@"You scored 99999" forKey:@"name"];
        [variables setObject:@" " forKey:@"caption"];
        [variables setObject:@"Download my app for the iPhone NOW." forKey:@"description"];

        FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/feed" withPostVars:variables];
        NSLog(@"postMeFeedButtonPressed:  %@", fb_graph_response.htmlResponse);

        //parse our json
        SBJSON *parser = [[SBJSON alloc] init];
        NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];   
        [parser release];

        NSLog(@"Now log into Facebook and look at your profile...");
    }

    [fbGraph release];
}

Solution 2

It looks like your permissions are a comma separated NSString, not an NSArray. The FB authorize: expects an NSArray, not an NSString.

Are you waiting for the response from the authenticate that calls fbGraphCallback ( is that code paraphrased?) I'm guessing your callback would show an auth failure because your perms are invalid. You have to wait for the auth to complete - otherwise you may not have the auth token before continuing.

FWIW - FB may have changed their rules in that app are not supposed to post to stream without letting a user modify the post text. A better route might be to go with your original code snip (waiting for auth if that's the issue?). You can put a link to your game in the post params and then put the score in the caption or description params (non-edit fields). You could then use the FB dialog and not let people change scores.

Solution 3

You can use Graph API.

[_facebook requestWithMethodName:@"stream.publish"
                   andParams:params
               andHttpMethod:@"POST"
                 andDelegate:nil];
Share:
23,121

Related videos on Youtube

theDuncs
Author by

theDuncs

iOS developer with an internet addiction.

Updated on July 09, 2022

Comments

  • theDuncs
    theDuncs almost 2 years

    I'm writing an iPhone game and want to publish the user's score to their facebook feed.

    I've managed to knock together an example where the user agrees with the authorisation and then a dialog appears which they can confirm to publish on their wall, or not publish. This is almost ideal, except that the text is an editable field - so the user could amend their score and then publish. Ideally, I want the exact same mechanism, but without the ability to amend the message.

    I'm assuming to do this, I would need to ask publish_stream permissions, followed by a Graph api call to post the message. I sourced this, but get an error 'An active access token must be used to query information about the current user.'.

    I'll happily take a point in the right direction over the actual code change - any help much appreciated.

    This is my first stackOverflow post, so be gentle please.

    Thanks guys.

    -Duncan

    Original code (which publishes to wall but with amendable textbox)

     //offer to facebook connect your score
     facebook = [[Facebook alloc] initWithAppId:@"210645928948875"];
     [facebook authorize:nil delegate:self];
    
     NSMutableString *facebookMessage = [NSMutableString stringWithString:@"I scored a whopping "];
     [facebookMessage appendString: [NSMutableString stringWithFormat:@"%d", currentScore]];
     [facebookMessage appendString: [NSMutableString stringWithString:@".  Can you beat me?"]];
    
     NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
     @"210645928948875", @"app_id",
     @"http://duncan.co.uk/", @"link",
     @"http://d.yimg.com/gg/goran_anicic/dunc.jpeg", @"picture",
     @"dunc", @"name",
     //@"Reference Documentation", @"caption",
     @"Download the app NOW from the App Store", @"description",
     facebookMessage,  @"message",
     nil];
    
     [facebook dialog:@"stream.publish" andParams:params andDelegate:self];
    

    Code to publish direct to wall (not proved) (which raises active token error):

    /*Facebook Application ID*/
    NSString *client_id = @"210645928948875";
    
    //alloc and initalize our FbGraph instance
    self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
    
    //begin the authentication process..... andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access"
    [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access"];  
    
    NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
    
    [variables setObject:@"the message" forKey:@"message"];
    [variables setObject:@"http://duncan.co.uk" forKey:@"link"];
    [variables setObject:@"bold copy next to image" forKey:@"name"];
    [variables setObject:@"plain text score." forKey:@"description"];
    
    FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/feed" withPostVars:variables];
    NSLog(@"postMeFeedButtonPressed:  %@", fb_graph_response.htmlResponse);
    
  • The Mad Gamer
    The Mad Gamer about 13 years
    I mentioned the authorize: method you seem to have an older version of the SDK
  • theDuncs
    theDuncs about 13 years
    Thanks so much Mad Gamer! I found the solution yesterday and will post it now, but you were pretty much on the right track - the code under the fbGraphCallback line should have been in the fbGraphCallback function.
  • theDuncs
    theDuncs about 13 years
    Also, the permissions work fine when listed as a comma-delimited string. No problem there. Thanks so much for your help though. Really appreciate it.
  • Satyam
    Satyam almost 13 years
    Can I pass local images for link and picture options? If yes, how?
  • Chris Masterton
    Chris Masterton almost 13 years
    No local links, the URL needs to be accessible from the internet.

Related