我看过很多关于如何解决这个问题的不同帖子,但我没有运气。我已经尝试过心跳解决方案,但它什么也没做。我知道我的钥匙串正在存储我的刷新令牌,但它没有任何用途。
脚步:
- 启动应用
- 转到加载目录(在这种情况下为根目录)
- 得到这个错误:
编辑:首先我得到一个 20000 错误。看来我的身份验证令牌没有刷新。
Error Domain=com.box.sdk.errordomain Code=20002 “操作无法完成。(com.box.sdk.errordomain 错误20002。)”
- 再次执行 Box 登录过程。
- 重新加载表格视图
- 作品。
我正在使用此代码来刷新我的访问令牌(我认为它应该)
if (storedRefreshToken)
{
[BoxSDK sharedSDK].OAuth2Session.refreshToken = storedRefreshToken;
}
我觉得我在这里也错过了一些东西。
我需要我的用户在允许的 14 天内保持登录状态。如何获取应用程序登录状态以在应用程序重新启动后继续存在?
我正在使用最新的 V2 SDK。
编辑:
我已经尝试了一切,从刷新每个 ViewController 上的钥匙串中的 refreshtoken 到引用 AppDelegate。当我再次启动应用程序(不是恢复,而是冷启动)时,我无法让它保持登录状态并且一直收到 20002 错误。我不想使用 Box 文件选择器,但我想制作自己的表格视图。还有其他想法吗?
AppDelegate:
in didFinishLaunching:
[BoxSDK sharedSDK].OAuth2Session.clientID = @"XXXXXXXXXX";
[BoxSDK sharedSDK].OAuth2Session.clientSecret = @"XXXXXXX";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boxAPITokensDidRefresh:) name:BoxOAuth2SessionDidBecomeAuthenticatedNotification object:[BoxSDK sharedSDK].OAuth2Session];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setRefreshTokenInKeychain:) name:BoxOAuth2SessionDidRefreshTokensNotification object:[BoxSDK sharedSDK].OAuth2Session];
// set up stored OAuth2 refresh token
_keychain = [[KeychainItemWrapper alloc] initWithIdentifier:REFRESH_TOKEN_KEY accessGroup:nil];
id storedRefreshToken = [_keychain objectForKey:(__bridge id)kSecValueData];
if (storedRefreshToken)
{
[BoxSDK sharedSDK].OAuth2Session.refreshToken = storedRefreshToken;
}
侦听器方法
- (void)boxAPITokensDidRefresh:(NSNotification *)notification
{
BoxOAuth2Session *OAuth2Session = (BoxOAuth2Session *) notification.object;
[self setRefreshTokenInKeychain:OAuth2Session.refreshToken];
_isBox = YES;
[self removeBoxLoginViewController];
}
- (void)setRefreshTokenInKeychain:(NSString *)refreshToken
{
[_keychain setObject:@"MyApp" forKey: (__bridge id)kSecAttrService];
[_keychain setObject:refreshToken forKey:(__bridge id)kSecValueData];
NSLog(@"refreshToken: %@", refreshToken);
}
主视图控制器:ViewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boxAPIAuthenticationDidSucceed:) name:BoxOAuth2SessionDidBecomeAuthenticatedNotification object:[BoxSDK sharedSDK].OAuth2Session];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boxAPIAuthenticationDidFail:) name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification object:[BoxSDK sharedSDK].OAuth2Session];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boxAPIAuthenticationRefreshToken:) name:BoxOAuth2SessionDidReceiveRefreshErrorNotification object:[BoxSDK sharedSDK].OAuth2Session];
[self boxAPIHeartbeat];
心跳:
- (void)boxAPIHeartbeat
{
[[BoxSDK sharedSDK].foldersManager folderInfoWithID:@"0" requestBuilder:nil success:nil failure:nil];
}
听音后的 ListenerMethods:
- (void)boxAPIAuthenticationDidSucceed:(NSNotification *)notification
{
NSLog(@"Received OAuth2 successfully authenticated notification");
BoxOAuth2Session *session = (BoxOAuth2Session *) [notification object];
NSLog(@"Access token (%@) expires at %@", session.accessToken, session.accessTokenExpiration);
NSLog(@"Refresh token (%@)", session.refreshToken);
//[self.tableView reloadData];
}
- (void)boxAPIAuthenticationDidFail:(NSNotification *)notification
{
NSLog(@"Received OAuth2 failed authenticated notification");
NSString *oauth2Error = [[notification userInfo] valueForKey:BoxOAuth2AuthenticationErrorKey];
NSLog(@"Authentication error (%@)", oauth2Error);
//[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)boxAPIAuthenticationRefreshToken:(NSNotification *)notification
{
BoxOAuth2Session *OAuth2Session = (BoxOAuth2Session *) notification.object;
[self setRefreshTokenInKeychain:OAuth2Session.refreshToken];
NSLog(@"REFRESH TOKEN: %@", OAuth2Session.refreshToken);
}
//trying this out????
- (void)setRefreshTokenInKeychain:(NSString *)refreshToken
{
[_keychain setObject:@"MyApp" forKey: (__bridge id)kSecAttrService];
[_keychain setObject:refreshToken forKey:(__bridge id)kSecValueData];
NSLog(@"refreshToken: %@", refreshToken);
}
如果这个周末我不能解决这个问题,我就不能使用 Box SDK。我认为 Box 会希望他们的 SDK 被开发人员使用,但是文档太差了。我错过了什么?我只希望应用程序通过冷启动保持登录状态!