Exemple #1
0
        public async Task <T> Post <T>(object body, string apiRelativeUrl)
        {
            HttpRequestMessage request = await _requestBuilder.CreateRequestAsync(HttpMethod.Post, body, apiRelativeUrl);

            HttpResponseMessage response = await _httpService.SendAndReturnResponseAsync(request);

            await VerifyResponseAsync(response);

            return(await _responseReader.ReadResponseAsync <T>(response));
        }
        private async Task <TokenResponse> GetNewAccessTokenAsync()
        {
            const string apiUrl  = "/v1/oauth2/token";
            var          request = new HttpRequestMessage(HttpMethod.Post, _configuration["Paypal:ApiBaseUrl"] + apiUrl);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US"));

            request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                          Convert.ToBase64String(
                                                                              Encoding.ASCII.GetBytes($"{_configuration["Paypal:ClientId"]}:{_configuration["Paypal:Secret"]}")));

            request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/json");

            HttpResponseMessage response = await _httpService.SendAndReturnResponseAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                string responseContent = await response.Content.ReadAsStringAsync();

                throw new Exception("Failed when getting a new access token. Paypal responded with: " + response + ". Content: " + responseContent);
            }

            return(await _responseReader.ReadResponseAsync <TokenResponse>(response));
        }