1

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

奇怪的是,对于我的网站来说,它运行良好。我只是使用回调 url 将令牌发布到服务器。

所以在代码中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];
    }
}

然后,在登录控制器中收到通知:

- (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 原生生成的访问令牌。我以前从未像这样进行过FB登录。如果你能帮助我,那就太好了,无论如何,谢谢!

4

1 回答 1

1

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

它允许您使用主动访问令牌,几乎可以替代我。服务器为网站使用旧的 facebook 策略(带有 passpostjs 的策略)和用于 iOS 登录的 facebookstrategy-token。

于 2013-04-08T09:05:23.703 回答