2

I am fetching a facebook token from my server (where it was stored from a previous login on another device). I am using that to open a session and then I am making a relatively simple call with

[FBWebDialogs presentRequestsDialogModallyWithSession:[FBSession activeSession]
                                                  message:@"Message"
                                                    title:@"Title"
                                               parameters:params
                                                  handler:^(FBWebDialogResult result,
                                                  NSURL *resultURL, NSError *error) {}}];

The web window that pops up has a message saying "An Error occured. Please try again later.". I click OK and I trigger the callback (which I omitted above). When I trace the resultURL I get

fbconnect://success?error_code=110&error_msg=Missing+user+cookie+%28to+validate+session+user%29

I can use the session to post on my wall just fine. What am I missing?

4

3 回答 3

0

我发现 Facebook 重新调整的令牌包含其他元数据,例如随后被所有 Web 视图使用的 c_user cookie。设置远程缓存系统时,您必须在远程服务器上存储到期日期和刷新日期,并将它们返回以在 TokenData 对象中使用。如果这没有正确完成,则不会设置正确的元数据。

于 2013-07-30T15:40:51.490 回答
0

我已经 在我的应用程序中使用 Facebook sdk 3.5.3集成了嵌入式 webview

我希望我的代码能给你一些想法......

-(IBAction)fblogin:(id)sender{
         NSArray * arr=[NSArray                arrayWithObjects:@"publish_actions",@"email",@"basic_info",@"user_location",@"user_birthday",@"user_likes", nil];

        Interest_Status=[NSString stringWithFormat:@"%@",txt_interest.text];
            [FBSession  setActiveSession:session];

        if (FBSession.activeSession.isOpen) {
            if ([btn_Login.titleLabel.text isEqualToString:@"Logout"]) {

                [btn_Login setTitle:@"Login" forState:UIControlStateNormal];
                [btn_Login setImage:[UIImage imageNamed:@"FBLogin.png"] forState:UIControlStateNormal];


                FBSessionTokenCachingStrategy *tokenCachingStrategy = [[FBSessionTokenCachingStrategy alloc]
                                                                       initWithUserDefaultTokenInformationKeyName:nil];

                tokenCachingStrategy=nil;
                FBSession *seession=[FBSession activeSession];
                [seession closeAndClearTokenInformation];
                [session close];
                [FBSession setActiveSession:nil];
                session=nil;

                session=[[FBSession alloc]initWithPermissions:arr];
                self.ProfileLale.hidden=YES;


            }
            /*else if ([btn_Login.titleLabel.text isEqualToString:@"Login"]) {
                    [self updateForSessionChangeForSlot:1];
            }*/


        }else{
            [session openWithBehavior:FBSessionLoginBehaviorForcingWebView
                    completionHandler:^(FBSession *session,
                                        FBSessionState status,
                                        NSError *error) {
                            // this handler is called back whether the login succeeds or fails; in the
                            // success case it will also be called back upon each state transition between
                            // session-open and session-close
                        if (error)
                                {
                                    NSLog(@"error=%@ \n\n description=%@ \n\n,",error,error.description);
                                    [self switchToNoActiveUser];
                                }else{
                                    [self updateForSessionChangeForSlot:1];
                                }
                    }];

        }
 }




- (void)updateForSessionChangeForSlot:(int)slot {
   if (session.isOpen) {
                // fetch profile info such as name, id, etc. for the open session
                // Fetch user data
            FBRequest *me = [[FBRequest alloc] initWithSession:session
                                                     graphPath:@"me"];

            [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                             NSDictionary<FBGraphUser> *user,
                                             NSError *error) {
                    // because we have a cached copy of the connection, we can check
                    // to see if this is the connection we care about; a prematurely
                    // cancelled connection will short-circuit here
                    //   if (me != self.pendingRequest) {
                    //      return;
                    //  }
                  NSLog(@"user=%@",user);

                    //  self.pendingRequest = nil;
                    //  self.pendingLoginForSlot = -1;

                    // we interpret an error in the initial fetch as a reason to
                    // fail the user switch, and leave the application without an
                    // active user (similar to initial state)
                if (error) {
                    NSLog(@"error=%@",error);
                    NSLog(@"Couldn't switch user: %@", error.localizedDescription);
                    [self switchToNoActiveUser];
                    return;
                }else{
                     NSLog(@"user=%@",user);
                     [btn_Login setTitle:@"Logout" forState:UIControlStateNormal];
                     [btn_Login setImage:[UIImage imageNamed:@"FBLogout.png"] forState:UIControlStateNormal];

                }
            }];

        } else {
                // in the closed case, we check to see if we picked up a cached token that we
                // expect to be valid and ready for use; if so then we open the session on the spot
            if (session.state == FBSessionStateCreatedTokenLoaded) {
                    // even though we had a cached token, we need to login to make the session usable
                [session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
                    [self updateForSessionChangeForSlot:slot];
                }];
            }
        }

  }



- (void)switchToNoActiveUser {

      /*UIAlertView *alter=[[UIAlertView alloc]initWithTitle:@"Message" message:@"No Internet connecton" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alter show];
       */
         NSArray * arr=[NSArray             arrayWithObjects:@"publish_actions",@"email",@"basic_info",@"user_location",@"user_birthday",@"user_likes", nil];

        session = nil;
        session=[[FBSession alloc]initWithPermissions:arr];
        [btn_Login setTitle:@"Login" forState:UIControlStateNormal];
        self.ProfileLale.hidden=YES;
        [btn_Login setImage:[UIImage imageNamed:@"FBLogin.png"] forState:UIControlStateNormal];

    }
于 2013-08-01T11:49:54.973 回答
0

确保您对活动的 Facebook 会话具有“publish_actions”权限。

    bool hasPublishPermission = NO;
    for( NSString* permission in [FBSession.activeSession permissions] )
    {
        if( [permission isEqualToString:@"publish_actions"] )
        {
            hasPublishPermission = YES;
            break;
        }
    }
    if( !hasPublishPermission )
    {
        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"]
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                                             if( status==FBSessionStateOpen )
                                             {
                                                 //Present dialog
                                             }
                                         }];
    }
    else
    {
        //Present dialog
    }

此外,请确保您已将“FacebookAppID”添加到项目的 plist 中。(请参阅https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/上的“配置新 Xcode 项目”选项卡)这可能会导致您描述的错误。

希望这可以帮助

于 2013-07-31T14:17:24.080 回答