예제 #1
0
        public static async Task <string> RenewAccessToken(AccessTokenUpdatingArg tokenUpdatingArg)
        {
            Console.WriteLine("старт скачивания данных о новом токене \n\n\n");
            HttpClient httpClient = new HttpClient();
            Dictionary <string, string> requestData = new Dictionary <string, string>
            {
                { "grant_type", "refresh_token" },
                { "client_id", tokenUpdatingArg.ClientId },
                { "client_secret", tokenUpdatingArg.ClientSecret },
                { "refresh_token", tokenUpdatingArg.RefreshToken }
            };

            HttpContent  httpContent = new FormUrlEncodedContent(requestData);
            const string url         = "https://accounts.google.com/o/oauth2/token";

            var responseMessage = await httpClient.PostAsync(url, httpContent);

            string responseContent = await responseMessage.Content.ReadAsStringAsync();

            Console.WriteLine($"{nameof(responseMessage.StatusCode)} {responseMessage.StatusCode}");
            Console.WriteLine($"{nameof(responseContent)} {responseContent}");

            if (responseMessage.IsSuccessStatusCode)
            {
                Console.WriteLine($"responseMessage is ok status");
                return(responseContent);
            }

            throw new Exception("1 Не удалось получить токен.");
        }
예제 #2
0
        public static async Task <RefreshedData> UpdateAccessToken(AccessTokenUpdatingArg tokenUpdatingArg)
        {
            Console.WriteLine("Обновление access token");
            string responseContent = await CustomGoogleApiAccessTokenUpdater.RenewAccessToken(tokenUpdatingArg);

            dynamic       responseObj = JsonConvert.DeserializeObject(responseContent);
            int           expiresIn   = responseObj.expires_in;
            string        accessToken = responseObj.access_token;
            RefreshedData result      = new RefreshedData
            {
                AccessToken  = accessToken,
                ExpiresInSec = expiresIn
            };

            return(result);
        }
예제 #3
0
        private async Task StartEndlessAccessTokenUpdatingAsync(AccessTokenUpdatingArg tokenUpdatingArg, int firstDelaySec = 2)
        {
            int delaySec = firstDelaySec;

            while (true)
            {
                await Task.Delay(1000 *delaySec);

                RefreshedData result = TokenManagerService.UpdateAccessToken(tokenUpdatingArg).Result;
                lock (lockObj)
                {
                    accessToken = result.AccessToken;
                }
                delaySec = result.ExpiresInSec;
            }
            // ReSharper disable once FunctionNeverReturns
        }
예제 #4
0
        public async Task Initialize()
        {
            GoogleApiProfile googleApiProfile = profileStorageService.GetCurrentProfile();

            try
            {
                GoogleApiAuthData authData;
                bool haveRefreshToken = googleApiProfile.GoogleApiData != null;
                if (haveRefreshToken)
                {
                    Console.WriteLine("Refresh token уже есть");
                    authData = JsonConvert
                               .DeserializeObject <GoogleApiAuthData>(googleApiProfile.GoogleApiData);
                }
                else
                {
                    Console.WriteLine("Создание нового refresh токена");
                    InitializeAccessTokenArg initializeAccessTokenArg = googleApiProfile.GetGoogleApiInitArg();
                    authData = await TokenManagerService.CreateRefreshTokenAsync(initializeAccessTokenArg);

                    string text = JsonConvert.SerializeObject(authData);
                    Console.WriteLine(text);
                    throw new Exception("Введите refresh token в текущий google api profile " + text);
                }

                if (authData != null)
                {
                    AccessTokenUpdatingArg test = new AccessTokenUpdatingArg()
                    {
                        ClientId     = googleApiProfile.ClientId,
                        ClientSecret = googleApiProfile.ClientSecret,
                        RefreshToken = authData.RefreshToken
                    };
                    Task task = StartEndlessAccessTokenUpdatingAsync(test);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
            }
        }