コード例 #1
0
        private HttpWebRequest CreateRequest(string endPoint, string method, string accept = "application/json", string query = null)
        {
            var uri = new UriBuilder(_baseUri)
            {
                Path = endPoint,
            };

            if (!string.IsNullOrWhiteSpace(query))
            {
                uri.Query = query;
            }

            var request = (HttpWebRequest)WebRequest.Create(uri.Uri);

            request.Timeout = defaultTimeout;

            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            request.Accept = accept;
            request.Method = method;

            if (ModifiedSince.HasValue)
            {
                request.IfModifiedSince = ModifiedSince.Value;
            }

            // add OAuth2 headers
            if (_auth != null)
            {
                //var oauthSignature = _auth.GetSignature(Consumer, User, request.RequestUri, method, Consumer);

                //AddHeader("Authorization", oauthSignature);


                //var clientId = ConfigurationManager.AppSettings["XeroClientId"];
                //var secret = ConfigurationManager.AppSettings["XeroSecret"];
                //_identityClient = new RestClient("https://identity.xero.com/");
                //var userPass = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Consumer.ConsumerKey}:{Consumer.ConsumerSecret}"));
                //AddHeader("authorization", "Basic {userPass}");

                var token = _auth.GetToken(null, null);

                if (token.HasExpired)
                {
                    _xeroClient.AuthToken = token as XeroOAuthToken;

                    token = _xeroClient.RefreshToken();
                    _tokenStore.Add(token);
                }
                AddHeader("Authorization", $"Bearer {token.TokenKey}");

                AddHeader("Xero-tenant-id", _auth.TenantId);
            }

            AddHeaders(request);

            request.UserAgent = !string.IsNullOrWhiteSpace(UserAgent) ? UserAgent : "Xero Api wrapper - " + Consumer.ConsumerKey;

            if (_rateLimiter != null)
            {
                _rateLimiter.WaitUntilLimit();
            }

            return(request);
        }