Ejemplo n.º 1
0
        private async Task <string> RefreshAsync()
        {
            string   responseJson;
            DateTime tokenCreateTimeUtc;

            using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, tokenUri))
            {
                requestMessage.Content = new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    ["type"]          = "refresh",
                    ["refresh_token"] = refreshToken,
                    ["client_id"]     = oauthOptions.ClientId,
                    ["redirect_uri"]  = oauthOptions.RedirectUrl,
                    ["client_secret"] = oauthOptions.ClientSecret,
                });
                requestMessage.Headers.UserAgent.TryParseAdd(userAgent);

                using (var httpClient = httpClientPool.Create())
                    using (var responseMessage = await httpClient.SendAsync(requestMessage))
                    {
                        await HttpResponseUtils.EnsureSuccessAsync(responseMessage);

                        tokenCreateTimeUtc = DateTime.UtcNow;
                        responseJson       = await responseMessage.Content.ReadAsStringAsync();
                    }
            }

            var tokenResponse = JsonConvert.DeserializeObject <TokenResponse>(responseJson);

            this.accessToken = tokenResponse.AccessToken;
            this.estimatedAccessTokenExpirationUtc = ParseExpiresIn(tokenResponse.ExpiresIn, tokenCreateTimeUtc);
            return(accessToken);
        }
Ejemplo n.º 2
0
        private async Task <IPageCollection <T> > GetPageCollectionAsync <T>(Uri uri, string accessToken, CancellationToken cancellationToken)
        {
            IResponseMessageCacheEntry responseMessageCacheEntry = null;

            if (responseMessageCache != null)
            {
                responseMessageCacheEntry = await responseMessageCache.GetAsync(uri);
            }

            await rateLimiter.WaitIfNecessaryAsync();

            string responseJson;
            Uri    nextUri;

            using (var requestMessage = CreateRequest(HttpMethod.Get, uri, accessToken: accessToken, responseMessageCacheEntry: responseMessageCacheEntry))
                using (var httpClient = httpClientPool.Create())
                    using (var responseMessage = await httpClient.SendAsync(requestMessage, cancellationToken))
                    {
                        var localResponseMessage = responseMessage;
                        if (responseMessage.StatusCode == System.Net.HttpStatusCode.NotModified && responseMessageCacheEntry != null)
                        {
                            localResponseMessage = responseMessageCacheEntry.ToResponseMessage();
                        }
                        else
                        {
                            await HttpResponseUtils.EnsureSuccessAsync(responseMessage);
                        }
                        responseJson = await localResponseMessage.Content.ReadAsStringAsync();

                        nextUri = HttpResponseUtils.ParseNextUriOrNull(localResponseMessage);

                        if (responseMessageCacheEntry == null)
                        {
                            await CacheResponseMessageAsync(uri, localResponseMessage, responseJson);
                        }
                    }

            var elements = JsonConvert.DeserializeObject <IReadOnlyCollection <T> >(responseJson);

            return(new PageCollection <T>(elements, nextUri, this));
        }