skuvault-integrations / etsyAccess

MIT License
12 stars 5 forks source link

Example of use Etsy API #20

Open Zubastic opened 3 years ago

Zubastic commented 3 years ago

Hello, I trying to use library but don't understand how get token and tokenSecret into EtsyConfig class.

Could you share your sample of code? I guess, first of all I should auth with etsyAuthenticationService but I must provide tokens into config class. What is my mistake?

My sample:

            var config = new EtsyConfig("ZubasticShop", "???", "???");

            // API key and secret from https://www.etsy.com/developers/your-apps
            var factory = new EtsyServicesFactory("key", "token");
            var throttler = new Throttler(config.ThrottlingMaxRequestsPerRestoreInterval, config.ThrottlingRestorePeriodInSeconds, config.ThrottlingMaxRetryAttempts);

            var etsyAuthenticationService = factory.CreateAuthenticationService(config);
            var credentials = await etsyAuthenticationService.GetTemporaryCredentials( new[] { "listings_w listings_r transactions_r" } );

            var etsyOrdersService = factory.CreateOrdersService(config, throttler);
            var startDate = DateTime.Now.AddDays(-1);
            var endDate = DateTime.Now;

            var orders = etsyOrdersService.GetOrders(startDate, endDate, CancellationToken.None);

Also, I guess it would be best to change GetTemporaryCredentials method signature from string[] to enum Flags (and convert to string array before send).

Zubastic commented 3 years ago

Finally, I did it:

            var config = new EtsyConfig(); // Empty config for first login

            // API key and secret from https://www.etsy.com/developers/your-apps
            var factory = new EtsyServicesFactory("Keystring", "Shared secret");
            var throttler = new Throttler(config.ThrottlingMaxRequestsPerRestoreInterval, config.ThrottlingRestorePeriodInSeconds, config.ThrottlingMaxRetryAttempts);

            var etsyAuthenticationService = factory.CreateAuthenticationService(config);
            var temporaryCredentials = await etsyAuthenticationService.GetTemporaryCredentials( new[] { "listings_w listings_r transactions_r" } );
            // Open printed link in browser and get access code.
            Console.WriteLine(temporaryCredentials.LoginUrl);
            // Enter code from step above 
            var verifierCode = Console.ReadLine();
            // Save token and tokenSecret in file. Operation above one time to use.
            var credentials = await etsyAuthenticationService.GetPermanentCredentials(temporaryCredentials.Token, temporaryCredentials.TokenSecret, verifierCode);

            // Recreate config with tokens above
            config = new EtsyConfig("<Shop Name>", credentials.Token, credentials.TokenSecret);

            var etsyOrdersService = factory.CreateOrdersService(config, throttler);
            var startDate = DateTime.Now.AddDays(-1);
            var endDate = DateTime.Now;

            // Do your stuff
            var orders = await etsyOrdersService.GetOrdersAsync(startDate, endDate, CancellationToken.None);