gpt4 book ai didi

iphone - 从 iOS 发布到 Facebook 时间线的第一次尝试会出现 HTTP 400

转载 作者:行者123 更新时间:2023-11-29 04:24:31 24 4
gpt4 key购买 nike

我正在遵循 facebook SDK 上提供的教程:

Login with facebook using the ios SDK

Publish to Feed

一切似乎都工作正常,除了测试我的应用程序之外,我在第一次尝试发布到 Facebook 墙时收到 HTTP 错误 400(或错误代码 5)。如果我在应用程序中再次按下“发布”按钮,第二次,一切似乎都正常。第一次尝试时,用户被发送到 facebook 应用程序进行身份验证,然后切换回应用程序,然后给我 HTTP 400。第二次尝试时,没有应用程序切换,并且消息按预期发布到 facebook 墙.

我试图弄清楚为什么我的应用程序在第一次尝试时不会发布到墙上/时间线。我的代码与教程中的代码相同。

编辑:

忘了提及,我使用一个按钮来进行身份验证并发布到墙上 - 这是 IBAction 中的代码:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// The user has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES];


[FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
{

NSString *alertText;

if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d", error.domain, error.code];
} else {
/*alertText = [NSString stringWithFormat:
@"Posted action, id: %@", [result objectForKey:@"id"]];*/
alertText = @"Posted!";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
}];

最佳答案

我通过 mkral 的有用注释解决了这个问题 - 代码更改如下:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

if (FBSession.activeSession.isOpen) { //session is open so can post right away

[FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
{

NSString *alertText;

if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d", error.domain, error.code];
} else {
alertText = @"Posted!";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
}];

}
else //session isn't open so authenticate first, then can post when back to app through notification
{
NSLog(@"Facebook Active Session not open");
// The user has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES];
}

因此,我首先检查是否有事件 session - 如果有,我可以将其发布到墙/时间线,如果没有,我会打开一个 session 。然后,我注册了一个通知(在 viewDidLoad 中),让我知道是否有 session 更改(在本例中,如果 session 打开,就会发生更改),并且一旦经过身份验证,它就会发布到墙上。

- (void)viewDidLoad
{
[super viewDidLoad];

//register for the session change notification you defined in the app delegate (for example when session changes to logged in)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(sessionStateChanged:)
name:FBSessionStateChangedNotification
object:nil];

}

- (void)sessionStateChanged:(NSNotification*)notification {
if (FBSession.activeSession.isOpen) {

[FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
{

NSString *alertText;

if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d", error.domain, error.code];
} else {
alertText = @"Posted!";
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
}];

}
}

关于iphone - 从 iOS 发布到 Facebook 时间线的第一次尝试会出现 HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12534736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com