我在尝试从刷新令牌中获取新的访问令牌时遇到了一些麻烦。我正在使用带有 swift 4 的本机应用程序。
登录控制器看起来像
func checkAccessToken() {
SessionManager.shared.refreshToken { error in
DispatchQueue.main.async {
guard error == nil else {
print("Failed to renew credentials: \(String(describing: error))")
return self.showLock()
}
SessionManager.shared.retrieveProfile { error in
DispatchQueue.main.async {
guard error == nil else {
print("Failed to retrieve profile: \(String(describing: error))")
return self.showLock()
}
self.performSegue(withIdentifier: "HomeView", sender: nil)
}
}
}
}
}
会话管理器看起来像:
func refreshToken(_ callback: @escaping (Error?) -> ()) {
guard let refreshToken = self.keychain.string(forKey: self.keyRefresh) else {
return callback(SessionManagerError.noRefreshToken)
}
Auth0
.authentication()
.renew(withRefreshToken: refreshToken)
.start { result in
switch (result) {
case .success(let credentials):
self.storeTokens(credentials.accessToken)
callback(nil)
case .failure(let error):
self.logout()
callback(error)
}
}
}
和retrieveProfile:
func retrieveProfile(_ callback: @escaping (Error?) -> ()) {
guard let accessToken = self.keychain.string(forKey: self.keyAccessToken) else {
return callback(SessionManagerError.noAccessToken)
}
Auth0
.authentication()
.userInfo(token: accessToken)
.start { result in
switch(result) {
case .success(let profile):
self.profile = profile
callback(nil)
case .failure(let error):
callback(error)
}
}
}
refreshToken返回真。因此,理论上,访问令牌是新的。然后我尝试使用新的访问令牌再次检索配置文件,而RetrieveProfile的结果是一个错误:
Failed to retrieve profile: Optional({"sub":"****","email":"*****@gmail.com","email_verified":true})
状态码为 0