Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining Access token from refresh token using Google API

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.

like image 892
Naren Avatar asked Nov 03 '22 20:11

Naren


1 Answers

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);
        }
    }
like image 176
NAM Avatar answered Nov 10 '22 23:11

NAM