Aditya Mathur
Aditya Mathur

Reputation: 1185

Facebook SDK 3.1 error when posting from iOS app

I'm facing the following problems with my iOS app:

  1. While publishing to user's friend's wall, using iOS app I'm getting the error "com.facebook.sdk error 5".

  2. The app is asking to login every time when I check for "FBSession.activeSession.isOpen".

Everything was working fine before. But since I've changed my app ID (as now I have to use different Facebook app). I'm getting the error.

I've done some research on this and found some solutions. Ive tried them all but nothing works.

I've written the following code to login to 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];
                                          }];
}

And the following code for publishing story:

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

When I run the app I get the following error:

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}

Please help me to understand where I'm going wrong.

I've also added some key-value pair to NSUserDefaults. Could that be a possible reason for that?

Thanks.

Upvotes: 2

Views: 5232

Answers (1)

NikGreen
NikGreen

Reputation: 700

When you're first showing your view with post button, perhaps you want to check if the session is open and set your post button title according to session state. Assuming your button initially has "Post" title:

[FBSession openActiveSessionWithAllowLoginUI: NO];

if (![FBSession.activeSession isOpen]) 
{
    [self setSendButtonTitle:NSLocalizedString(@"Log in",@"")];
}

Then add the following code to your Post button action:

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

So if the user presses send and the app is not authorized, it will perform a login operation and change your Post button title from "Log in" to "Post". Then the user will be able to post the message by pressing the button once again.

Hope it helps

Upvotes: 1

Related Questions