title: AWS Cognito: dealing with token expiration time
body: <p>I am experimenting with Cognito and when I thought it was starting to be OK, I am facing the issue of (Google) token expiring after 1 hour.</p>
  
  <p>When I start with a clean device, I can sign up, use the app for 1 hour, then as soon as I need to refresh a dataset, I get and error saying the the token is not authorised.</p>
  
  <p>Are there any example on how to deal with this?</p>
  
  <p>Is it really something the app developers should be doing?
  I was expecting the SDK to manage such things in the background.</p>
  
  <p>Is it meaning that we have to check the credentialsProvider.getSessionCredentitalsExpiration() before each dataset synchronisation ?</p>
  
  <p>Many thanks,
  JM</p>
  
  <p><strong>Edit 1 : Added code</strong></p>
  
  <p>I do have a SigninActivity, but it is only called when no credentials exists at all, in theory only once the first time the user logs in.</p>
  
  <p>It is built as follows (removed useless bits).
  So what happens is that I authenticate right the first time, but as I never enter this activity again, maybe something is missing.</p>
  
  <p>But there must be a way to refresh this token silently?</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.signin);
  
      // Aws Credential provider
      credentialsProvider = new CognitoCachingCredentialsProvider(
              getApplicationContext(), // Context
              getString(R.string.aws_identity_pool), // Identity Pool ID
              Regions.EU_WEST_1 // Region
      );
  
      // Google connect
      findViewById(R.id.signin_with_google_btn).setOnClickListener(this);
  
      GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
              .requestEmail()
              .requestProfile()
              .requestIdToken(getString(R.string.google_server_client_id))
              .requestId()
              .build();
  
      // Build a GoogleApiClient with access to the Google Sign-In API and the
      // options specified by gso.
      googleApiClient = new GoogleApiClient.Builder(this)
              .enableAutoManage(this, this )
              .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
              .build();
  
      SignInButton signInButton = (SignInButton) findViewById(R.id.signin_with_google_btn);
      signInButton.setSize(SignInButton.SIZE_WIDE);
      signInButton.setScopes(gso.getScopeArray());
      signInButton.setColorScheme(SignInButton.COLOR_DARK);
  }
  
  @Override
  public void onClick(View view) {
      this.signinWithGoogle();
  }
  
  /**
   * Triggers Google signin
   */
  private void signinWithGoogle() {
      Log.v(this, "Signing in with Google...");
  
      Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
      this.startActivityForResult(signInIntent, GOOGLE_SIGN_IN);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == GOOGLE_SIGN_IN) {
          GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
          this.handleGoogleSignInResult(result);
      } else {
          // Other stuff
      }
  }
  
  
  /**
   * Handle Google sign in result
   */
  private void handleGoogleSignInResult(GoogleSignInResult result) {
  
      if (result.isSuccess()) {
  
          Log.v(this, "Successfully logged in with Google...");
  
          GoogleSignInAccount acct = result.getSignInAccount();
          Log.v(this, "Signed in as %s / %s (token %s)", acct.getDisplayName(), acct.getEmail(), acct.getIdToken());
          Map&lt;String, String&gt; logins = new HashMap&lt;&gt;();
          logins.put("accounts.google.com", acct.getIdToken());
          Log.v(SigninActivity.this, "Google token &lt;&lt;&lt;\n%s\n&gt;&gt;&gt;", logins.get("accounts.google.com"));
          credentialsProvider.setLogins(logins);
      } else {
          // Signed out
          Log.w(this, "Failed to authenticate against Google #%d - %s", result.getStatus().getStatusCode(), result.getStatus().getStatusMessage());
  
          SimpleMessageDialog.show(SigninActivity.this,
                  R.drawable.icon,
                  R.string.error,
                  R.string.sorry_error_signing_you_in_please_try_again,
                  R.string.try_again);
      }
  }
  </code></pre>