public async Task Refresh()
        {
            if (RefreshToken == null)
            {
                throw new Exception("CurrentCredentials does not have a refresh token");
            }

            TokenLifecycleManager tlcm          = new TokenLifecycleManager();
            TokenResponse         tokenResponse = await tlcm.RefreshTokenAsync(AuthState.CreateTokenRefreshRequest()).ConfigureAwait(false);

            AuthState.Update(tokenResponse, null);
        }
        public async Task <string> GetActiveAccessToken()
        {
            if (AuthState.NeedsTokenRefresh)
            {
                try
                {
                    TokenRequest  request  = AuthState.CreateTokenRefreshRequest();
                    TokenResponse response = await AuthService.PerformTokenRequestAsync(request);

                    AuthState.Update(response, null);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }

            return(AuthState.AccessToken);
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.activity_token);

            authService = new AuthorizationService(this);

            if (savedInstanceState != null)
            {
                if (savedInstanceState.ContainsKey(KEY_AUTH_STATE))
                {
                    try
                    {
                        authState = AuthState.JsonDeserialize(savedInstanceState.GetString(KEY_AUTH_STATE));
                    }
                    catch (JSONException ex)
                    {
                        Console.WriteLine("Malformed authorization JSON saved: " + ex);
                    }
                }

                if (savedInstanceState.ContainsKey(KEY_USER_INFO))
                {
                    try
                    {
                        userInfoJson = new JSONObject(savedInstanceState.GetString(KEY_USER_INFO));
                    }
                    catch (JSONException ex)
                    {
                        Console.WriteLine("Failed to parse saved user info JSON: " + ex);
                    }
                }
            }

            if (authState == null)
            {
                authState = GetAuthStateFromIntent(Intent);
                AuthorizationResponse  response = AuthorizationResponse.FromIntent(Intent);
                AuthorizationException ex       = AuthorizationException.FromIntent(Intent);
                authState.Update(response, ex);

                if (response != null)
                {
                    Console.WriteLine("Received AuthorizationResponse.");
                    PerformTokenRequest(response.CreateTokenExchangeRequest());
                }
                else
                {
                    Console.WriteLine("Authorization failed: " + ex);
                }
            }

            RefreshUi();

            var refreshTokenButton = FindViewById <Button>(Resource.Id.refresh_token);

            refreshTokenButton.Click += delegate
            {
                PerformTokenRequest(authState.CreateTokenRefreshRequest());
            };

            var viewProfileButton = FindViewById <Button>(Resource.Id.view_profile);

            viewProfileButton.Click += delegate
            {
                Task.Run(() => FetchUserInfo());
            };
        }