Beispiel #1
0
        public static async Task <string> GetAuthData(InitializeAccessTokenArg initAccessToken)
        {
            HttpClient httpClient = new HttpClient();
            Dictionary <string, string> requestData = new Dictionary <string, string>()
            {
                { "grant_type", "authorization_code" },
                { "code", initAccessToken.Code },
                { "client_id", initAccessToken.ClientId },
                { "client_secret", initAccessToken.ClientSecret },
                { "redirect_uri", initAccessToken.RedirectUri }
            };

            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("2 Не удалось получить токен.");
        }
        public InitializeAccessTokenArg GetGoogleApiInitArg()
        {
            InitializeAccessTokenArg initializeAccessTokenArg = new InitializeAccessTokenArg
            {
                Code         = Code,
                ClientId     = ClientId,
                ClientSecret = ClientSecret,
                RedirectUri  = RedirectUri
            };

            return(initializeAccessTokenArg);
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        public static async Task <GoogleApiAuthData> CreateRefreshTokenAsync(InitializeAccessTokenArg initAccessToken)
        {
            string responseContent = await CustomGoogleApiInitializer.GetAuthData(initAccessToken);

            dynamic responseObj = JsonConvert.DeserializeObject(responseContent);

            // string tokenType = responseObj.token_type;
            int    expiresIn    = responseObj.expires_in;
            string accessToken  = responseObj.access_token;
            string refreshToken = responseObj.refresh_token;

            GoogleApiAuthData result = new GoogleApiAuthData
            {
                AccessToken             = accessToken,
                RefreshToken            = refreshToken,
                ExpiresInSec            = expiresIn,
                AccessTokenCreationTime = DateTime.UtcNow
            };

            return(result);
        }