I have an installed c# app with code working that gets the authorization code and exchanges it for an access token. I am storing off the refresh token. I know at some point I need to use it to get a new access token. Let's assume that I am periodically calling the following method to monitor the files that have been shared with my Drive account.
/// <summary>
/// Retrieve a list of File resources.
/// </summary>
/// <param name="service">Drive API service instance.</param>
/// <returns>List of File resources.</returns>
public static List<File> retrieveAllFiles(DriveService service) {
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
request.Q = "sharedWithMe and trashed=false";
do {
try {
FileList files = request.Fetch();
result.AddRange(files.Items);
request.PageToken = files.NextPageToken;
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
request.PageToken = null;
}
} while (!String.IsNullOrEmpty(request.PageToken));
return result;
}
}
I assume that at some point the call to service.Files.List() is going to fail. How do I know it has failed due to an expired access token and what is the code to use the refresh token? I already have some code (below) that I gleaned from here to use the refresh token. Will this method get called when the access token expires?
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// If we already have a RefreshToken, use that
if (!string.IsNullOrEmpty(RefreshToken))
{
state.RefreshToken = RefreshToken;
if (arg.RefreshToken(state)) {
mTextBox.Text = "RF: " + RefreshToken;
return state;
}
}
// authCode is a TextBox on the form
var result = arg.ProcessUserAuthorization(mTextBox.Text, state);
RefreshToken = state.RefreshToken;
return result;
}