iOS Swift,AWS Cognito用户池,无法刷新访问令牌 [英] iOS Swift, AWS Cognito User Pool, Unable to Refresh Access Token

查看:65
本文介绍了iOS Swift,AWS Cognito用户池,无法刷新访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看过类似的SO问题,这些问题无法帮助我解决问题。

Looked at similar SO questions which didn't help me resolve the issue.

我正在iOS应用中使用AWS Cognito用户池。我们能够成功创建并登录用户。但是在大约Hr之后访问令牌不可用,我从AWS Cognito文档中了解到iOS SDK自动刷新(此处也提到了),并在不可用时获取令牌,但是我看不到这种行为。以下代码显示了我如何尝试获取访问令牌。

I am using AWS Cognito User Pools in our iOS App. We are able to successfully create and login the user. However after about an Hr the access token is not available, I understand from AWS Cognito documentation that the iOS SDK automatically refreshes (also mentioned here) and obtains the token when it is not available, however I don't see this behaviour. The below code shows how I am trying to obtain the access token.

使用iOS SDK AWSCognitoIdentityProvider 2.6.7

Using iOS SDK AWSCognitoIdentityProvider 2.6.7

请告知我如何解决此问题。

Please advice how I can resolve the issue.

let mySession = self.pool.currentUser()?.getSession()

guard let accessToken = mySession?.result?.accessToken?.tokenString as? String else {
  print("Unable to obtain access token")
  self.handleSignOut() // Signing out the user since there is no access token       
  return
}


推荐答案

getSession()返回一个AWSTask。

getSession() returns an AWSTask.

您必须在回调中访问 tokenString

以下代码对我有用:

self.pool.currentUser()?.getSession().continueWith { task in

    if task.error == nil {
        let session = task.result! as AWSCognitoIdentityUserSession
        guard let accessToken = session.idToken?.tokenString; as? String else {
           print("Unable to obtain access token")
           self.handleSignOut() // Signing out the user since there is no access token       
        }
    }
}

这篇关于iOS Swift,AWS Cognito用户池,无法刷新访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆