示例#1
0
        public static HarvestClient Create()
        {
            var client = HarvestClient.FromAccessToken("TestClient", "<access token>");

            client.DefaultAccountId = null;
            return(client);
        }
示例#2
0
        public async Task <bool> VerifyConfiguration()
        {
            try
            {
                var token = Environment.GetEnvironmentVariable(HARVEST_DEVELOPER_TOKEN);
                if (String.IsNullOrWhiteSpace(token))
                {
                    throw new NotSupportedException(
                              $"Must set environment variable {HARVEST_DEVELOPER_TOKEN}, see https://id.getharvest.com/developers to get one");
                }

                client = HarvestClient.FromAccessToken("SunnyHarvestTool", token);

                var accounts = await client.GetAccountsAsync();

                if (accounts == null || accounts.Accounts == null)
                {
                    throw new NotSupportedException("Empty accounts package");
                }
                if (accounts.Accounts.Length > 1)
                {
                    throw new NotSupportedException("Do not handle multiple accounts yet");
                }
                Console.WriteLine($"Using Account {accounts.Accounts[0].Id}={accounts.Accounts[0].Name}");
                client.DefaultAccountId = accounts.Accounts[0].Id;

                me = await client.GetMe();

                if (me == null)
                {
                    throw new Exception("unexpected null result");
                }
                Console.WriteLine($"Logged in as {me.FirstName} {me.LastName} ({me.Id})");

                ReadMapFiles();

                return(true);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Could not verify configuration - {ex.Message}");
                return(false);
            }
        }