title: How to get new access token in c#?
body: <p>I'm working on reading/sending gmail emails in c# with API requests. I have got an access token but it expires after one hour and further actions are not executed anymore. Could, please, someone help me with the procedure on how to get a new valid access token?</p>
  <p>Thank you in advance!</p>
  <p>EDIT:</p>
  <p>This is a code from where I get authorization:</p>
  <pre><code>public static GmailService AuthenticateOauthGmail(string clientSecretJson, string userName)
      {
          try
          {
              if (string.IsNullOrEmpty(userName))
                  throw new ArgumentNullException(&quot;userName&quot;);
              if (string.IsNullOrEmpty(clientSecretJson))
                  throw new ArgumentNullException(&quot;clientSecretJson&quot;);
              if (!File.Exists(clientSecretJson))
                  throw new Exception(&quot;clientSecretJson file does not exist.&quot;);
  
              // These are the scopes of permissions you need. It is best to request only what you need and not all of them
              string[] scopes = new string[] { &quot;https://mail.google.com&quot;};
  
              //UserCredential credential;
              using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
              {
                  string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                  credPath = Path.Combine(credPath, &quot;.credentials/&quot;, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
  
  
                  // Requesting Authentication or loading previously stored authentication for userName
                  Variables.Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                                                                           scopes,
                                                                           userName,
                                                                           CancellationToken.None,
                                                                           new FileDataStore(credPath, true)).Result;
                  var newCredentials = GoogleWebAuthorizationBroker.ReauthorizeAsync(Variables.Credential, CancellationToken.None);
              }
  
              // Create Drive API service.
              return new GmailService(new BaseClientService.Initializer()
              {
                  HttpClientInitializer = Variables.Credential,
                  ApplicationName = &quot;Gmail Oauth2 Authentication Sample&quot;
              });
          }
          catch (Exception ex)
          {
              Console.WriteLine(&quot;Create Oauth2 account GmailService failed&quot; + ex.Message);
              throw new Exception(&quot;CreateServiceAccountGmailFailed&quot;, ex);
          }
      }
  </code></pre>
  <p>This is how my Credentials.Token looks like
  <a href="https://i.stack.imgur.com/vAOBG.jpg" rel="nofollow noreferrer">credentials.Token</a></p>
  <p>Since access token is expired, when trying to read mails, I get an error:</p>
  <pre><code>{
  &quot;error&quot;: {
      &quot;code&quot;: 401,
      &quot;message&quot;: &quot;Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.&quot;,
      &quot;errors&quot;: [
          {
              &quot;message&quot;: &quot;Invalid Credentials&quot;,
              &quot;domain&quot;: &quot;global&quot;,
              &quot;reason&quot;: &quot;authError&quot;,
              &quot;location&quot;: &quot;Authorization&quot;,
              &quot;locationType&quot;: &quot;header&quot;
          }
      ],
      &quot;status&quot;: &quot;UNAUTHENTICATED&quot;
     }
  }
  </code></pre>
  <p>I have nuget package Google.Apis.Auth. Should I use another one?</p>