简体   繁体   English

AWSMobileClient:访问令牌未自动刷新

[英]AWSMobileClient: Access Token not refreshed automatically

Documentation says: "The tokens are automatically refreshed by the library when necessary.".文档说:“令牌在必要时由库自动刷新。”。 I call我打电话

AWSMobileClient.getInstance().getTokens().getAccessToken().getTokenString(); everytime before calling the API with AccessToken.每次在使用 AccessToken 调用 API 之前。 Here is what my awsconfiguration file looks like这是我的 awsconfiguration 文件的样子

`{
"IdentityManager": {
    "Default": {}
  },
"CredentialsProvider": {
    "CognitoIdentity": {
        "Default": {
            "PoolId": "ap-xxxxx-1:2xxxxxx-xxxx-xxxx-xxxx-7xxxxxxxxxx",
            "Region": "ap-xxxxx-1"
        }
    }
},

"CognitoUserPool": {
    "Default": {
        "PoolId": "ap-xxxxx-1_xxxxxxxxx",
        "AppClientId": "xxxxxxxxxxxxxxxxxxxxx",
        "Region": "ap-xxxxx-1"
    }
}

After an hour, API returns 401(Authentication Error).一个小时后,API 返回 401(身份验证错误)。 And AWSMobileClient.getInstance().getTokens().getAccessToken().getTokenString();AWSMobileClient.getInstance().getTokens().getAccessToken().getTokenString(); returns an error AWSMobileClient: Tokens are invalid, please sign-in again. java.lang.Exception: No cached session.返回错误AWSMobileClient: Tokens are invalid, please sign-in again. java.lang.Exception: No cached session. AWSMobileClient: Tokens are invalid, please sign-in again. java.lang.Exception: No cached session.

Am I doing something wrong?难道我做错了什么? PS: I have not used Amplify CLI. PS:我没有使用过 Amplify CLI。

So the problem was I was calling the getTokens() method from the main thread and it returned the tokens without refreshing.所以问题是我从主线程调用 getTokens() 方法,它在没有刷新的情况下返回了令牌。 Then I moved the getTokens call inside the AWSMobileClient.getInstance().initialize(..) call and I got refreshed tokens whenever required.然后我将 getTokens 调用移到 AWSMobileClient.getInstance().initialize(..) 调用中,并在需要时获得刷新的令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM