0

所以我最近才开始使用 iOS 开发,并且正在使用PodioKit库与 Podio 交互。使用电子邮件和密码向服务器进行身份验证后,它会返回一个 OAuth 令牌。这具有所需的数据、刷新令牌等。

为了让用户在使用应用程序之间保持登录状态,OAuth 令牌应该保存到钥匙串中,然后在应用程序启动时再次访问。我一直在使用JNKeychain将它们存储在钥匙串中,这似乎工作正常。但是,当我从钥匙串中检索令牌时,数据并不相同,这意味着 PodioKit 在我尝试做任何事情时都会抛出“刷新令牌丢失”错误。您可以看到这些 NSLog 输出的差异。

保存到钥匙串之前:

2014-01-23 15:40:38.069 PodioKPITest[377:60b] oauthToken: <PKOAuth2Token: 0x16d59390>
2014-01-23 15:40:38.071 PodioKPITest[377:60b] oauthToken.refData: {
    id = 1883826;
    type = user;
}
2014-01-23 15:40:38.072 PodioKPITest[377:60b] oauthToken.refreshToken: d9a59577e0574d20bdbc739ccfcf61ce
2014-01-23 15:40:38.073 PodioKPITest[377:60b] oauthToken.accessToken: 53909cabac874fb78fcca7eda87a4e84
2014-01-23 15:40:38.079 PodioKPITest[377:60b] oauthToken.expiresOn: 2014-01-23 12:40:38 +0000
2014-01-23 15:40:38.080 PodioKPITest[377:60b] oauthToken.transferToken: (null)

从钥匙串加载后:

2014-01-23 15:41:00.509 PodioKPITest[389:60b] oauthToken: <PKOAuth2Token: 0x14d42cb0>
2014-01-23 15:41:00.512 PodioKPITest[389:60b] oauthToken.refData: (null)
2014-01-23 15:41:00.514 PodioKPITest[389:60b] oauthToken.refreshToken: (null)
2014-01-23 15:41:00.516 PodioKPITest[389:60b] oauthToken.accessToken: (null)
2014-01-23 15:41:00.521 PodioKPITest[389:60b] oauthToken.expiresOn: 2014-01-23 04:40:38 +0000
2014-01-23 15:41:00.523 PodioKPITest[389:60b] oauthToken.transferToken: (null)

有谁知道为什么会这样?是否有比钥匙串更好/更正确的存储令牌的地方?希望我已经解释清楚了!

4

1 回答 1

0

是的,钥匙串是存储令牌的正确位置,因为它将被加密。

我尝试了您对 Podio 和 JNKeychain 所做的完全相同的事情,对我来说它确实有效。您使用的是哪个版本的 PodioKit 和 JNKeychain?在您的情况下,取消归档似乎无法恢复这些属性值。

PKOAuth2Token *token = [[PKOAuth2Token alloc] initWithAccessToken:@"access1234"
                                                     refreshToken:@"refresh1234"
                                                    transferToken:@"transfer1234"
                                                        expiresOn:[NSDate dateWithTimeIntervalSinceNow:3600]
                                                          refData:@{@"ref": @"somedata"}];

// Test archiving
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:token];
PKOAuth2Token *archivedToken = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"Restored access token from archive %@", archivedToken.accessToken);

// Test keychain
NSString *keychainKey = @"AuthToken";
[JNKeychain saveValue:token forKey:keychainKey];
PKOAuth2Token *keychainToken = [JNKeychain loadValueForKey:keychainKey];
NSLog(@"Restored access token from keychain: %@", keychainToken.accessToken);

以上打印:

2014-01-23 21:07:22.719 App[17202:70b] Restored access token from archive access1234
2014-01-23 21:07:22.723 App[17202:70b] Restored access token from keychain: access1234

你总是可以尝试一个替代的钥匙串包装库,比如FXKeychain

于 2014-01-23T20:11:42.077 回答