如何在不修改消息的情况下从 iOS 应用程序发布到 Facebook 墙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5377584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to publish from iOS application to facebook wall without user amending message
提问by theDuncs
I'm writing an iPhone game and want to publish the user's score to their facebook feed.
我正在编写 iPhone 游戏并希望将用户的分数发布到他们的 Facebook 提要。
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.'.
我假设这样做,我需要询问 publish_stream 权限,然后调用 Graph api 来发布消息。我找到了这个,但收到错误“必须使用活动访问令牌来查询有关当前用户的信息。”。
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.
这是我的第一篇 stackOverflow 帖子,所以请保持温和。
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);
采纳答案by theDuncs
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.
找到解决方案。应首先调用身份验证。一旦通过身份验证,这将调用 fbGraphCallback 函数,该函数将执行用户流的发布。
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.
所有这一切都是由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];
}
回答by The Mad Gamer
It looks like your permissions are a comma separated NSString, not an NSArray. The FB authorize:
expects an NSArray, not an NSString.
看起来您的权限是逗号分隔的 NSString,而不是 NSArray。FBauthorize:
需要一个 NSArray,而不是一个 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.
您是否正在等待来自调用的身份验证的响应fbGraphCallback
(该代码是否解释过?)我猜您的回调会显示身份验证失败,因为您的权限无效。您必须等待身份验证完成 - 否则在继续之前您可能没有身份验证令牌。
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.
FWIW - FB 可能已经更改了该应用程序中的规则,该应用程序不应该在不让用户修改帖子文本的情况下发布到流媒体。更好的方法可能是使用您的原始代码片段(如果这是问题,请等待身份验证?)。您可以在 post params 中放置一个指向您的游戏的链接,然后将分数放在 title 或 description params(非编辑字段)中。然后您可以使用 FB 对话框,而不是让人们更改分数。
回答by Payman hodaie
You can use Graph API.
您可以使用图形 API。
[_facebook requestWithMethodName:@"stream.publish"
andParams:params
andHttpMethod:@"POST"
andDelegate:nil];