我的 iOS 应用程序面临以下问题:
在使用 iOS 应用程序发布到用户 friend 的墙上时,我收到错误“
com.facebook.sdk error 5
”。每次我检查“
FBSession.activeSession.isOpen
”时,应用程序都要求登录。
之前一切正常。但是因为我已经更改了我的应用程序 ID(因为现在我必须使用不同的 Facebook 应用程序)。我遇到了错误。
我对此做了一些研究,发现 some solutions .我都试过了,但没有任何效果。
我编写了以下代码来登录 Facebook:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
comingfromFB = YES;
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"friends_birthday",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
发布故事的代码如下:
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self publishStory];
}
}];
} else {
// If permissions present, publish the story
[self publishStory];
}
当我运行该应用程序时,出现以下错误:
Error: HTTP status code: 403
2013-02-05 14:36:47.109 <appname>[1705:c07] Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xaa9af20 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 200;
message = "(#200) The user hasn't authorized the application to perform this action";
type = OAuthException;
};
};
code = 403;
}, com.facebook.sdk:HTTPStatusCode=403}
请帮助我了解哪里出了问题。
我还在 NSUserDefaults
中添加了一些键值对。这可能是造成这种情况的原因吗?
谢谢。
最佳答案
当您第一次显示带有发布按钮的 View 时,您可能想要检查 session 是否打开并根据 session 状态设置您的发布按钮标题。假设您的按钮最初有“发布”标题:
[FBSession openActiveSessionWithAllowLoginUI: NO];
if (![FBSession.activeSession isOpen])
{
[self setSendButtonTitle:NSLocalizedString(@"Log in",@"")];
}
然后将以下代码添加到您的“发布”按钮操作中:
- (void) send: (UIButton*) sender
{
if (![FBSession.activeSession isOpen])
{
[FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: @"publish_stream", nil]
defaultAudience: FBSessionDefaultAudienceEveryone
allowLoginUI: YES
completionHandler: ^(FBSession *session,
FBSessionState status,
NSError *error)
{
if (error)
{
NSLog(@"error");
}
else
{
[FBSession setActiveSession:session];
[self setSendButtonTitle: NSLocalizedString(@"Post",@"")];
}
}];
return;
}
// here comes the code you use for sending the message
}
因此,如果用户按下发送按钮并且该应用未获得授权,它将执行登录操作并将您的发布按钮标题从“登录”更改为“发布”。然后用户将能够通过再次按下按钮来发布消息。
希望对你有帮助
关于iphone - 从 iOS 应用程序发布时出现 Facebook SDK 3.1 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704050/