2

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. Here is what my awsconfiguration file looks like

`{
"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). And AWSMobileClient.getInstance().getTokens().getAccessToken().getTokenString(); returns an error 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.

1 Answers1

1

So the problem was I was calling the getTokens() method from the main thread and it returned the tokens without refreshing. Then I moved the getTokens call inside the AWSMobileClient.getInstance().initialize(..) call and I got refreshed tokens whenever required.