iPhone - Box SDK/API 20000 error followed by 20002 Error - Authentication tokens are not refreshing
关于如何解决此问题,我看到了很多不同的文章,但是我没有运气。我已经尝试过心跳解决方案,但是它什么也没做。我知道我的钥匙串正在存储我的刷新令牌,但是它没有任何用处。
脚步:
编辑:首先我得到一个20000错误。看来我的身份验证令牌没有刷新。
Error Domain=com.box.sdk.errordomain Code=20002"The operation
couldn’t be completed. (com.box.sdk.errordomain error 20002.)"
我正在使用此代码刷新我的访问令牌(我认为应该这样做)
1 2 3 4 | if (storedRefreshToken) { [BoxSDK sharedSDK].OAuth2Session.refreshToken = storedRefreshToken; } |
我觉得我在这里也想念一些东西。
我需要我的用户在允许的14天内保持登录状态。如何获得应用程序登录状态以在应用程序重启后继续生存?
我正在使用最新的V2 SDK。
编辑:
我已经尝试了一切,从刷新每个ViewController上的钥匙串中的refreshtoken到引用AppDelegate。我无法保持登录状态,仅在再次启动应用程序时一直出现20002错误(不是恢复,而是冷启动)。我不想使用Box文件选择器,但是我想创建自己的表视图。还有其他想法吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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; } |
侦听器方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - (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); } |
主ViewController:
ViewDidLoad
1 2 3 4 | [[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]; |
心跳:
1 2 3 4 | - (void)boxAPIHeartbeat { [[BoxSDK sharedSDK].foldersManager folderInfoWithID:@"0" requestBuilder:nil success:nil failure:nil]; } |
监听之后的ListenerMethods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - (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,但是文档太差了。我想念什么?我只希望该应用通过冷启动保持登录状态!
事实证明,问题出在ARC版本的Keychain。 当我开始到处放置NSLogs时,我注意到了这一点,并注意到在应用程序启动时返回的refreshToken不是正在编码到钥匙串中的refreshToken。 我用示例应用程序中的文件替换了ARC钥匙串文件,并放入了ARC标志,它运行良好。