コード例 #1
0
        private async Task <string> CreateSpreadsheet(Auth0UserDto user, dynamic json)
        {
            var spreadsheetJson = new StringContent(JsonConvert.SerializeObject(json, Formatting.Indented,
                                                                                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }), Encoding.UTF8, "application/json");

            var client = _clientFactory.CreateClient();

            client.DefaultRequestHeaders.Add("Authorization",
                                             $"Bearer {user.Identities.FirstOrDefault(x => x.Provider == "google-oauth2")?.AccessToken}");
            using var httpResponse =
                      await client.PostAsync("https://sheets.googleapis.com/v4/spreadsheets", spreadsheetJson);

            if (httpResponse.IsSuccessStatusCode)
            {
                var responseAsString = await httpResponse.Content.ReadAsStringAsync();

                return(responseAsString);
            }

            if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
            {
                var refreshedUser = await _googleAuthService.GetNewAccessTokenAsync(user);

                if (_memoryCache.TryGetValue(user.UserId, out Auth0UserDto cachedUser))
                {
                    _memoryCache.Remove(user.UserId);
                }

                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetAbsoluteExpiration(
                    TimeSpan.FromSeconds(refreshedUser.Identities.FirstOrDefault()?.ExpiresIn ?? 0));

                // Save data in cache.
                _memoryCache.Set(user.UserId, user, cacheEntryOptions);
                return(await CreateSpreadsheet(refreshedUser, json));
            }

            return(null);
        }
コード例 #2
0
        public async Task <Auth0UserDto> GetNewAccessTokenAsync(Auth0UserDto user)
        {
            var client         = _clientFactory.CreateClient();
            var googleIdentity = user.Identities?.FirstOrDefault(x => x.Provider == "google-oauth2");

            if (googleIdentity != null)
            {
                var jsonContent = JsonConvert.SerializeObject(new
                {
                    client_id     = _configuration.ClientId,
                    client_secret = _configuration.ClientSecret,
                    refresh_token = googleIdentity?.RefreshToken,
                    grant_type    = "refresh_token"
                });
                var stringContent = new StringContent(jsonContent,
                                                      Encoding.UTF8, "application/json");

                client.BaseAddress = new Uri("https://oauth2.googleapis.com/token");

                var httpResponse = await client.PostAsync("/token", stringContent);

                if (httpResponse.IsSuccessStatusCode)
                {
                    var responseAsString = await httpResponse.Content.ReadAsStringAsync();

                    var authToken = JsonConvert.DeserializeObject <TokenResponseDto>(responseAsString);

                    googleIdentity.AccessToken = authToken.AccessToken;
                    googleIdentity.ExpiresIn   = authToken.ExpiresIn;

                    return(user);
                }
            }

            return(null);
        }