title: Expiry time of a login token through gmail api
body: <p>I have a testbed application that runs constantly to download amount of messages in the Inbox of a gmail account. I connect through gmail-api to request a login token and login to the account. The problem I am facing is that on each iteration the expiry time of the access token is 3600s, i.e. it never changes. Do I need to re-download the access token each time to see if it needs refreshing or does the api handle this for me?</p>
  <p><code>ImapClient</code> is part of the <code>AE.Net</code> library.</p>
  <p>See the below code:</p>
  <pre><code>try
  {
      X509Certificate2 certificate = new X509Certificate2(&quot;certificate.p12&quot;, &quot;notasecret&quot;, X509KeyStorageFlags.Exportable);
      ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential
          .Initializer(&quot;Service account address&quot;)
      {
          //Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
          Scopes = new[] { &quot;https://mail.google.com/&quot; },
          User = &quot;email address&quot;
      }.FromCertificate(certificate));
  
      Task&lt;bool&gt; result = credential.RequestAccessTokenAsync(CancellationToken.None);
      if (!result.Result)
      {
          Console.WriteLine(&quot;Failed to connect to gmail!&quot;);
      }
  
      using (ImapClient imap = new ImapClient())
      {
          imap.AuthMethod = ImapClient.AuthMethods.SaslOAuth;
  
          imap.Connect(&quot;imap.gmail.com&quot;, 993, true, false);
          if (!imap.IsConnected)
          {
              throw new Exception(&quot;Unable to connect to the host&quot;);
          }
  
          imap.Login(&quot;email address&quot;, credential.Token.AccessToken);
          if (!imap.IsAuthenticated)
          {
              throw new Exception(&quot;Currently not authenticated (2)?&quot;);
          }
  
          string sourceMailBox = &quot;Inbox&quot;;
          imap.SelectMailbox(sourceMailBox);
  
          while (true)
          {
              int messageCount = imap.GetMessageCount(sourceMailBox);
  
              Console.WriteLine(&quot;Messages in '&quot; + sourceMailBox + &quot;': &quot; + messageCount);
              Console.WriteLine(&quot;Token expires in {0}s&quot;, credential.Token.ExpiresInSeconds);
  
              System.Threading.Thread.Sleep(10000);
          }
      }
  }
  catch(Exception ex)
  {
      Console.Error.WriteLine(&quot;IMAP Exception: &quot; + ex.Message);
      Console.Error.WriteLine(ex.ToString());
  }
  </code></pre>
  <p>Example output (Note I am not writing out the GetResponse lines):</p>
  <blockquote>
  <p>GetResponse(): '* STATUS &quot;Inbox&quot; (MESSAGES 1)'</p>
  <p>GetResponse(): 'xm070 OK Success'</p>
  <p>Messages in 'Inbox': 1</p>
  <p>Token expires in 3600s</p>
  <p>GetResponse(): '* STATUS &quot;Inbox&quot; (MESSAGES 1)'</p>
  <p>GetResponse(): 'xm071 OK Success'</p>
  <p>Messages in 'Inbox': 1</p>
  <p>Token expires in 3600s</p>
  </blockquote>
  <p>After running for about 3100 seconds it gave me the following exception which I believe to be due to the token expiring.</p>
  <blockquote>
  <p>IMAP Exception: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.</p>
  <p>System.IO.IOException: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. ---&gt; System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine</p>
  <p>at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)</p>
  <p>at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)</p>
  <p>--- End of inner exception stack trace ---</p>
  <p>at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)</p>
  <p>at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)</p>
  <p>at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)</p>
  <p>at System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)</p>
  <p>at AE.Net.Mail.TextClient.SendCommand(String command)</p>
  <p>at AE.Net.Mail.ImapClient.OnLogout()</p>
  <p>at AE.Net.Mail.TextClient.Logout()</p>
  <p>at AE.Net.Mail.TextClient.Disconnect()</p>
  <p>at AE.Net.Mail.TextClient.Dispose(Boolean disposing)</p>
  <p>at AE.Net.Mail.ImapClient.Dispose(Boolean disposing)</p>
  <p>at AE.Net.Mail.TextClient.Dispose()</p>
  <p>at GmailOAuth.Program.DoWork() in</p>
  <p>c:\Projects\Development\EmailSystem\GmailOAuth\GmailOAuth\Program.cs:line 82</p>
  </blockquote>