title: How to refresh access token in C#
body: <p>I can't find a tutorial or a code example where it shows how/when to refresh the access token. How to implement refreshing the new access token when it is about to expire? </p>
  
  <p>This is what I have so far:</p>
  
  <p>This is the class where to get the new access token and refresh token.</p>
  
  <pre><code>namespace DemoApp
  {
      public class Tokens
      {
          public string access_token { get; set; }
          public string token_type { get; set; }
          public int expires_in { get; set; }
          public string refresh_token { get; set; }
      }
  
      public class AccessToken 
      {
          public void GetNewAccessToken()
          {
              Tokens localTokens = new Tokens();
  
              var client = new RestClient("www.example.com/api/token");
              var request = new RestRequest(Method.POST);
              request.AddHeader("content-type", "application/x-www-form-urlencoded");
              request.AddParameter("application/x-www-form-urlencoded", "grant_type=refresh_token&amp;refresh_token=" + localTokens.refresh_token, ParameterType.RequestBody);
              IRestResponse response = client.Execute(request);
              var responseContent = response.Content;
  
              var newTokensList = new JavaScriptSerializer().Deserialize&lt;Tokens&gt;(responseContent);
  
              localTokens.access_token = newTokensList .access_token;
              localTokens.refresh_token = newTokensList .refresh_token;
          }
      }
  }
  </code></pre>
  
  <p>This is the class where it call the API, with the new Access Token, to get the new data.</p>
  
  <pre><code>namespace DemoApp
  {
      public class API
      {
          public Data()
          {
              public void CallAPI()
              {
                  Tokens tokens = new Tokens();
                  var client = new RestClient("www.example.com/api");
                  var request = new RestRequest(Method.GET);
                  request.AddHeader("authorization", "Bearer " + tokens.access_token);
                  request.AddHeader("accept", "application/json; charset=utf-8");
                  IRestResponse response = client.Execute(request);
  
                  var data = response.Content;
              }
          }
      }
  }
  </code></pre>