繁体   English   中英

如何在没有用户修改消息的情况下从iOS应用程序发布到Facebook墙

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

我正在写一款iPhone游戏,并希望将用户的分数发布到他们的Facebook Feed中。

我已经成功地将用户同意授权的示例拼凑在一起,然后出现一个对话框,他们可以确认在他们的墙上发布,或者不发布。 这几乎是理想的,除了文本是可编辑的字段 - 因此用户可以修改他们的分数然后发布。 理想情况下,我想要完全相同的机制,但没有能力修改消息

我假设这样做,我需要询问publish_stream权限,然后使用Graph api调用来发布消息。 我采购了这个,但收到错误'必须使用活动访问令牌来查询有关当前用户的信息。'。

我很乐意在实际的代码更改中指出正确的方向 - 任何帮助都非常感激。

这是我的第一篇stackOverflow帖子,请温柔。

多谢你们。

-Duncan

原始代码(发布到墙上但带有可修改的文本框)

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

直接发布到墙上的代码(未证明)(引发活动令牌错误):

/*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);

找到解决方案 应首先调用身份验证。 经过身份验证后,这将调用fbGraphCallback函数,该函数将执行用户流的发布。

所有这一切都可以通过http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api来实现 奖励积分归于The Mad Gamer。 非常感谢。

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

看起来您的权限是逗号分隔的NSString,而不是NSArray。 FB authorize:期望NSArray,而不是NSString。

您是否正在等待来自调用fbGraphCallback的身份验证的响应(该代码是否已解释?)我猜您的回调会显示auth失败,因为您的perms无效。 您必须等待身份验证完成 - 否则您可能在继续之前没有身份验证令牌。

FWIW - FB可能已经更改了他们的规则,因为应用程序不应该在不让用户修改帖子文本的情况下发布到流。 一个更好的路线可能是你的原始代码剪辑(等待auth,如果这是问题?)。 您可以在post params中为游戏添加链接,然后将分数放在标题或描述参数(非编辑字段)中。 然后,您可以使用FB对话框,不要让人们改变分数。

您可以使用Graph API。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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