5

I have been using google+ api for .NET in my application.Using the example provided in this site i am able to get the access token.

My problem is how to obtain the access token from the refresh token using OAuth 2.0.I haven't found any examples to get the access token from refresh token.

I have referred the [google+ API reference] but they have mentioned it using HTTP methods.2Please provide me some examples in C# using the methods provided by google+ APIs.

Naren
  • 2,231
  • 1
  • 25
  • 45
  • 2
    http://stackoverflow.com/questions/11458644/how-to-generate-access-token-using-refresh-token-through-google-drive-sdk-in-ne – divyanshm Jun 18 '13 at 06:48
  • 1
    Thanks @divyanshm ,The link you provided has helped me to solve the problem.I have one doubt.Does this refresh token has expiration time?If it has,then how to find it out? – Naren Jun 18 '13 at 08:56
  • 2
    Refresh tokens, by definition, do not have an expiry time. They are valid till a user explicitly revokes permissions. Your app can save them permanently and exchange it for access tokens as and when required. – divyanshm Jun 18 '13 at 11:15
  • not true. refresh tokens do have an expiry time – Tal Avissar Jun 07 '17 at 15:25

1 Answers1

2

For the first time, you need to get the access token from the browser prompt and then save it in some store.

Check if the token is expired and then try to refresh it.

Code here :

private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
    {
        try
        {

            // Get the auth URL:
            var config = new Configuration();
            var calendarScope = Google.Apis.Util.Utilities.ConvertToString(CalendarService.Scopes.Calendar);
            IAuthorizationState state = new AuthorizationState(new[] { calendarScope });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);
            var authCode = String.Empty;

            if (String.IsNullOrWhiteSpace(config.AccessToken) || config.AccessTokenExpirationTime < DateTime.Now || String.IsNullOrWhiteSpace(config.RefreshToken))
            {

                // Request authorization from the user (by opening a browser window):
                Process.Start(authUri.ToString());
                do
                {
                  authCode = Prompt.ShowDialog("Test", "123");
                } while (String.IsNullOrWhiteSpace(authCode));
                state = arg.ProcessUserAuthorization(authCode, state);
            }
            else
            {
                state.AccessToken = config.AccessToken;
                state.AccessTokenExpirationUtc = config.AccessTokenExpirationTime;
                state.AccessTokenIssueDateUtc = config.AccessTokenIssueTime;
                state.RefreshToken =config.RefreshToken ;
                if (state.AccessTokenExpirationUtc < DateTime.Now)
                {
                    var tokenRefreshed = arg.RefreshToken(state);
                    if (tokenRefreshed)
                    {
                        config.AccessToken = state.AccessToken;
                        config.AccessTokenExpirationTime = state.AccessTokenExpirationUtc;
                        config.AccessTokenIssueTime = state.AccessTokenIssueDateUtc;
                        config.RefreshToken = state.RefreshToken;
                        arg.ProcessUserAuthorization(authCode, state);
                    }
                    else
                    {
                        throw new ApplicationException("Unable to refresh the token.");
                    }
                }
            }
            return state;
        }
        catch (System.Exception exp)
        {
            throw new ApplicationException("Failed to get authorisation from Google Calender.", exp);
        }
    }
NAM
  • 71
  • 4
  • What is the `NativeApplicationClient arg` parameter? In my case I am using the GMail.Send scope. I have the token file store in the user application data folder. Can i still use this method? – Andrew Truckle Jun 12 '19 at 21:17