GData OAuthUtil.GetAccessToken does not return a refresh_token value

.net, gdata, oauth

Solution

With the OAuth 2.0 offline flow, you only receive a refresh token the first time the user exchanges an authorization code and grants access to your app. You should store that refresh token in a database for later use.

If at any time you need a refresh token for the user and you don't have it in your database or the one you have is corrupt or revoked, you have to send the user back to the authorization page so that it can grant access again and provide you with a new refresh token.

The Google Drive SDK documentation explains this process thoroughly. Also, there's is a complete ASP.NET MVC application that you can use as reference.

Problem

When I use the `Google.GData.Client` .NET library and try to initiate an `OAuth` session it works fine until the session expires. It seems when I do my initial `GetAccessToken` request that the `RefreshToken` value is `null`. Step1 (default.aspx): ``` OAuth2Parameters parameters = new OAuth2Parameters(); //<<build parameters with keys and CLIENT_IDs and such>> string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); Response.Redirect(authorizationUrl); ``` Step2 (oauth.aspx): ``` Session["ACCESS_CODE"] = Request.QueryString["code"]; Response.Redirect("Results.aspx"); ``` Step3 (results.aspx) ``` if (Session["ACCESS_CODE"] == null) Response.Redirect("Default.aspx"); parameters.AccessCode = Session["ACCESS_CODE"].To_String(); OAuthUtil.GetAccessToken(parameters); Response.Write("OAuth Access Token: " + parameters.AccessToken); Session["ACCESS_TOKEN"] = parameters.AccessToken; Session["REFRESH_TOKEN"] = parameters.RefreshToken; ``` When I inspect the code at this point my `RefreshToken == null`. Because of this I cannot call ``` OAuthUtil.RefreshAccessToken(parameters); ``` without getting an error of value cannot be `null`.

Original source