我正在编写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);
答案 0 :(得分:12)
找到解决方案。应首先调用身份验证。经过身份验证后,这将调用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];
}
答案 1 :(得分:2)
您的权限看起来像是逗号分隔的NSString,而不是NSArray。 FB authorize:
期望NSArray,而不是NSString。
您是否正在等待来自调用fbGraphCallback
的身份验证的响应(该代码是否已解释?)我猜您的回调会显示auth失败,因为您的权限无效。您必须等待身份验证完成 - 否则您可能在继续之前没有身份验证令牌。
FWIW - FB可能已经改变了他们的规则,因为应用程序不应该在不让用户修改帖子文本的情况下发布到流。一个更好的路线可能是你的原始代码剪辑(等待auth,如果这是问题?)。您可以在post params中为游戏添加链接,然后将分数放在标题或描述参数(非编辑字段)中。然后,您可以使用FB对话框,不要让人们改变分数。
答案 2 :(得分:1)
您可以使用Graph API。
[_facebook requestWithMethodName:@"stream.publish"
andParams:params
andHttpMethod:@"POST"
andDelegate:nil];