ios - 无法在 iOS Facebook SDK 中将 access_token 验证到 PassportJS

标签 ios facebook node.js

我正在尝试将 Facebook iOS sdk 与 PassportJS 一起使用(服务器是 NodeJS) 我成功地在我的 iOS 应用程序上使用 Facebook 登录。接下来我要做的是将访问 token 交给服务器并从那里进行验证。不幸的是,它根本不起作用。

奇怪的是,我的网站运行良好。我只是使用回调 url 将 token 发布到服务器。

所以在代码中 facebook 登录后会发生什么(有效)

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSString *token = session.accessTokenData.accessToken;
                NSLog(@"User session found: %@", token);
                [[NSNotificationCenter defaultCenter] postNotificationName:nGotFacebookSession object:nil userInfo:@{
                    @"token": token
                }];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

然后,在登录 Controller 中收到通知:

- (void) facebookLogin: (NSNotification*) notification
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *token = [notification.userInfo objectForKey:@"token"];
    [[CBApi sharedAPI] get:@"api/auth/facebook/callback" andParams:@{
        @"code": token
    } andCallback:^(NSDictionary *json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([json objectForKey:kError]) {
            Alert(@"Error", @"Login details failed");
        } else {
            User *u = [User createEntity];
            u.email = self.emailField.text;
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

然后我从服务器返回这个错误(从控制台复制粘贴):

failed to obtain access token (status: 400 data: {
    "error": {
        "message": "Invalid verification code format.",
        "type": "OAuthException",
        "code": 100
    }
})

就像我说的,它在使用网站登录时完全有效,没问题,只是它不接受 iOS native 生成的访问 token 。我以前从来没有像这样登录过 fb。如果您能帮助我,那就太好了,无论如何,谢谢!

最佳答案

我想通了!对于遇到同样问题的任何人,这就是我所做的: 不要使用 passportjs 的普通 Facebook 策略,而是使用这个策略:https://github.com/drudge/passport-facebook-token

它允许您使用主动访问 token ,对我来说几乎是一个替代品。 服务器为网站使用旧的 facebook 策略(passpostjs 附带的策略)和用于 iOS 登录的 facebookstrategy-token。

关于ios - 无法在 iOS Facebook SDK 中将 access_token 验证到 PassportJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865933/

相关文章:

ios - RestKit 0.24 ||获取对象路径 ||结果对象的 NSString 参数设置为 NSNull

ios - 在 swift 中,如何在原型(prototype)单元格中将行(上面有按钮)推送到不同的 View Controller ?

ios - objective-c,获取pdf文档的页数

session - 找不到模块 'connect',当尝试使用 require ('connect' 时。utils.parseCookie 与 nodejs

ios - UILabel 从右向左和向后动画

api - 通过 API 创建 gmail、facebook 和 twitter 帐户?

css - 错误信息 : NoSuchElementException: Message: no such element: Unable to locate element: {"method" :"css selector" ,"selector" :"._5qtp"}

android - 将 facebook sdk 与 android 应用程序和网络服务器一起使用

python - 如何在 Node.js 中接受 POST 数据

node.js - Node.js 中的垃圾收集器