简体   繁体   English

无法在SDK 3.2中保存Facebook accessToken

[英]Can't save Facebook accessToken in SDK 3.2

I've been using the old Facebook SDK and saved the accessToken and expirationDate inside the NSUserDefaults . 我一直在使用旧的Facebook SDK,并将accessTokenexpirationDate保存在NSUserDefaults But now in the new Facebook SDK 3.2 there is a new class called FBAccessTokenData and I can take the accessToken from there by using: 但是现在,在新的Facebook SDK 3.2有了一个名为FBAccessTokenData的新类,我可以使用以下方法从其中获取accessToken:

[FBSession activeSession].accessTokenData.accessToken
[FBSession activeSession].accessTokenData.expirationDate

My problem is when I restart my app and click on Facebook button again, it goes to Facebook to ask for a new token although I used the app 3 minutes before, login into Facebook and got new token. 我的问题是,当我重新启动应用程序并再次单击Facebook按钮时,尽管我在3分钟前使用了该应用程序,登录Facebook并获得了新令牌,但它仍向Facebook询问了新令牌。 So the token hasn't been saved. 因此令牌尚未保存。

I'm trying to save the new accessToken into [FBSession activeSession] but accessToken and expirationDate are readonly properties. 我正在尝试将新的accessToken保存到[FBSession activeSession]但是accessTokenexpirationDate只读属性。

I've been reading the Facebook SDK documentation for few times now, searched stackOverflow and the web and still didn't got any clue how to do that. 我现在已经阅读Facebook SDK文档好几次了,搜索了stackOverflow和网络,但仍然不知道如何执行此操作。

This is my code: 这是我的代码:

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
    switch (state)
    {
        case FBSessionStateOpen:
        {
            // This is how i'm trying to save the token, like the old Facebook SDK.
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:[FBSession activeSession].accessTokenData.accessToken forKey:@"FBAccessTokenKey"];
            [defaults setObject:[FBSession activeSession].accessTokenData.expirationDate forKey:@"FBExpirationDateKey"];
            [defaults synchronize];

            NSLog(@"FB: Session is open!");
        }
            break;

        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
        {
            NSLog(@"FB: Session is closed or login failed");

            [self closeSession];
        }
            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)openSession
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error)
    {
         [self sessionStateChanged:session state:state error:error];
    }];
}

- (void)closeSession
{
    [FBSession.activeSession closeAndClearTokenInformation];
}

Thanks in advance! 提前致谢!

So I found the answer after searching more, If someone has problem with the new Facebook SDK 3.2 accessToken , look at this link . 因此,在搜索更多内容后,我找到了答案。如果有人对新的Facebook SDK 3.2 accessToken有问题,请查看此链接 and you'll find what you are looking for. 您会找到想要的东西。

Basically you create a class that handles the token saving and loading. 基本上,您将创建一个处理令牌保存和加载的类。 Than you save it into local plist file - pretty easy and simple. 与将其保存到本地plist文件相比,这非常容易和简单。

Enjoy! 请享用! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM