title: How to renew the access token using the refresh token?
body: <p>I am using <strong>ASP.NET MVC 5</strong> with <strong>OWIN</strong>.</p>
  
  <p>I have done a lot of research and haven't found how to renew the access token using the refresh token.</p>
  
  <p>My scenario is: The first time the user accesses my app, he or she grants access to the account I read the refresh token returned from the API. When the users come back to my app, I need to refresh the access token based on the "Refresh Token".</p>
  
  <p>Could anybody provide some code?</p>
  
  <p>Here is what I've achieved till now:</p>
  
  <p><strong>Startup.Auth.cs:</strong></p>
  
  <pre><code>    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
      {
          Caption = "Google+",
          ClientId = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientId,
          ClientSecret = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientSecret,
          CallbackPath = new PathString("/oauth-login-return"),
          Provider = new GoogleOAuth2AuthenticationProvider
          {
              OnAuthenticated = async context =&gt;
              {
                  context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name)));
                  context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Identity.FindFirstValue(ClaimTypes.Email)));
                  context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
                  context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
                  context.Identity.AddClaim(
                      new Claim(Parameters.Instance.Authentication.oAuth.GooglePlus.AccessTokenClaimType,
                          context.AccessToken));
              }
          }
      };
      googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
      googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
  </code></pre>
  
  <p><strong>AuthenticationController:</strong></p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  public ActionResult ExternalLogin(string provider, string returnUrl)
  {
      RedirectIfAuthenticated();
  
      return new ChallengeResult(provider, Url.Content("~/oauth-login-callback"));
  }
  
  [ActionName("oauth-login-back")]
  public async Task&lt;ActionResult&gt; ExternalLoginCallback(string returnUrl)
  {
  }
  
  // Used for XSRF protection when adding external logins
  private const string XsrfKey = "XsrfId";
  
  private IAuthenticationManager AuthenticationManager
  {
      get
      {
          return HttpContext.GetOwinContext().Authentication;
      }
  }
  
  private class ChallengeResult : HttpUnauthorizedResult
  {
      public ChallengeResult(string provider, string redirectUri)
          : this(provider, redirectUri, null)
      {
      }
  
      private ChallengeResult(string provider, string redirectUri, string userId)
      {
          LoginProvider = provider;
          RedirectUri = redirectUri;
          UserId = userId;
      }
  
      private string LoginProvider { get; set; }
  
      private string RedirectUri { get; set; }
  
      private string UserId { get; set; }
  
      public override void ExecuteResult(ControllerContext context)
      {
          var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
          if (UserId != null)
          {
              properties.Dictionary[XsrfKey] = UserId;
          }
          context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
      }
  }
  </code></pre>