FBWebDialogs 缺少用于验证用户的会话 Cookie

Posted

技术标签:

【中文标题】FBWebDialogs 缺少用于验证用户的会话 Cookie【英文标题】:FBWebDialogs is Missing Session Cookie to validate user 【发布时间】:2013-07-26 16:56:31 【问题描述】:

我正在从我的服务器获取一个 facebook 令牌(它是从另一个设备上的先前登录存储的)。我正在使用它来打开一个会话,然后我正在使用

进行一个相对简单的调用
[FBWebDialogs presentRequestsDialogModallyWithSession:[FBSession activeSession]
                                                  message:@"Message"
                                                    title:@"Title"
                                               parameters:params
                                                  handler:^(FBWebDialogResult result,
                                                  NSURL *resultURL, NSError *error) ];

弹出的网页窗口显示“发生错误。请稍后重试。”。我单击“确定”并触发回调(我在上面省略了)。当我跟踪 resultURL 时,我得到了

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

我可以使用会话在我的墙上发帖就好了。我错过了什么?

【问题讨论】:

你解决了这个问题吗? 哇,这是很久以前的事了。我不能说我做到了。对不起。 【参考方案1】:

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

【讨论】:

【参考方案2】:

确保您对活动的 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 项目”选项卡)这可能会导致您描述的错误。

希望对你有帮助

【讨论】:

【参考方案3】:

我已将 Embedded webview 与 Facebook sdk 3.5.3 集成到我的应用中

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

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

    

【讨论】:

抱歉,我应该提到我正在使用从我的服务器检索到的令牌创建会话,该令牌是从另一个设备的先前登录存储的...

以上是关于FBWebDialogs 缺少用于验证用户的会话 Cookie的主要内容,如果未能解决你的问题,请参考以下文章

在iOS上反应Native WebView缺少会话cookie

Nodejs Express 用于 Web 应用程序 JWT,带有数据库或会话以验证用户身份

用于 Spotify 的 Python 社交身份验证:重定向缺少“状态”参数

用于 Web 用户身份验证的 Java 选项

用于 Web 用户身份验证的 Java 选项

GraphQL:接受缺少请求字段的查询响应?