// GET: /Authorization/Callback
        public async Task <ActionResult> Callback(string code, string state)
        {
            var serviceProvider   = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            var httpClientFactory = serviceProvider.GetService <IHttpClientFactory>();

            XeroConfiguration XeroConfig = new XeroConfiguration
            {
                ClientId     = ConfigurationManager.AppSettings["XeroClientId"],
                ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
                CallbackUri  = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
                Scope        = ConfigurationManager.AppSettings["XeroScope"],
                State        = ConfigurationManager.AppSettings["XeroState"]
            };

            var client = new XeroClient(XeroConfig, httpClientFactory);

            var xeroToken = (XeroOAuth2Token)await client.RequestXeroTokenAsync(code);

            List <Tenant> tenants = await client.GetConnectionsAsync(xeroToken);

            Tenant firstTenant = tenants[0];

            TokenUtilities.StoreToken(xeroToken);

            return(RedirectToAction("Index", "OrganisationInfo"));
        }
コード例 #2
0
        public async Task <IActionResult> ConnectionAsync(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Authorization/Connection")] HttpRequest req
            , ILogger log
            )
        {
            var xeroToken = await _tokenStore.GetStoredToken();

            var tenants = await _client.GetConnectionsAsync(xeroToken);

            return(new JsonResult(tenants));
        }
コード例 #3
0
        // GET /Authorization/Callback
        public async Task <ActionResult> Callback(string code, string state)
        {
            var client    = new XeroClient(XeroConfig.Value, httpClientFactory);
            var xeroToken = (XeroOAuth2Token)await client.RequestXeroTokenAsync(code);

            List <Tenant> tenants = await client.GetConnectionsAsync(xeroToken);

            Tenant firstTenant = tenants[0];

            TokenUtilities.StoreToken(xeroToken);

            return(RedirectToAction("Index", "OrganisationInfo"));
        }
コード例 #4
0
        // GET /Authorization/Callback
        public async Task <ActionResult> Callback(string code, string state)
        {
            var client    = new XeroClient(XeroConfig.Value);
            var xeroToken = (XeroOAuth2Token)await client.RequestAccessTokenAsync(code);

            List <Tenant> tenants = await client.GetConnectionsAsync(xeroToken);

            Tenant firstTenant = tenants[0];

            TokenUtilities.StoreToken(xeroToken);

            return(Redirect("http://localhost:3000/home"));
        }
コード例 #5
0
        public async Task <IXeroToken> GetTokensAsync(string code, string state)
        {
            var client = new XeroClient(_xeroConfig, _httpClientFactory);

            //before getting the access token please check that the state matches
            _xeroToken = await client.RequestXeroTokenAsync(code);

            //from here you will need to access your Xero Tenants
            List <Tenant> tenants = await client.GetConnectionsAsync(_xeroToken);

            _xeroToken.Tenants = tenants;
            return(_xeroToken);
        }
コード例 #6
0
        public void Run(string name)
        {
            try
            {
                Console.WriteLine(name + ": Starting ");
                var clientId = _config["ClientId"];

                XeroClient client = null;
                IXeroToken token  = null;
                lock (_lockObj)
                {
                    client = new XeroClient(new XeroConfiguration {
                        ClientId = clientId
                    });

                    if (System.IO.File.Exists("Token.json"))
                    {
                        var savedJson = System.IO.File.ReadAllText("Token.json");
                        token = JsonConvert.DeserializeObject <XeroOAuth2Token>(savedJson);
                    }

                    if (token == null)
                    {
                        token = new XeroOAuth2Token {
                            RefreshToken = _config["RefreshToken"]
                        }
                    }
                    ;

                    IXeroToken newToken = Task.Run(() => client.RefreshAccessTokenAsync(token)).GetAwaiter().GetResult();
                    if (newToken != null)
                    {
                        token = newToken;
                    }

                    var json = JsonConvert.SerializeObject(token, Formatting.Indented);
                    System.IO.File.WriteAllText("Token.json", json);
                }

                Console.WriteLine(name + ": Token refreshed");
                var accessToken = token.AccessToken;

                var tenant = "";
                try
                {
                    var conRes = Task.Run(() => client.GetConnectionsAsync(token)).GetAwaiter().GetResult();
                    tenant = conRes[0].TenantId.ToString();
                    Console.WriteLine(name + ": Tenants");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    SerializeException(e);
                    return;
                }

                var api = new AccountingApi {
                    ExceptionFactory = CustomExceptionFactory
                };

                Console.WriteLine(name + ": New token process");

                var response     = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var respInvoices = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant, page: 1, where : "")).GetAwaiter().GetResult();
                var invDate      = respInvoices.Data._Invoices[0].Date;

                var recurring = Task.Run(() => api.GetRepeatingInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var status    = recurring.Data._RepeatingInvoices[0].Status;
                var schedule  = recurring.Data._RepeatingInvoices[0].Schedule;

                Console.WriteLine(name + ": Complete");
            }
            catch (CustomApiException ce)
            {
                Console.WriteLine(name + ": Failed: " + ce.Message);
                SerializeException(ce);
                Console.WriteLine(ce);
                var limits = new XeroLimits(ce.Headers);
            }
            catch (Exception e)
            {
                Console.WriteLine(name + ": Failed: " + e.Message);
                SerializeException(e);
            }
        }