Use Etsy API with OAuth 1.0 in ASP.NET Core (C#)

Manuel Reinfurt
3 min readNov 8, 2020

The Etsy API still relies on OAuth 1.0 and there’s not a lot of material out there explaining on how to use it — besides the official documentation which can be a bit short at times.

In this article I’m going to show you how you can easily use the Etsy API in ASP.NET Core using C#. The example is also fully compatibel with ASP.NET 5.

The flow

The goal is to get the access token in order to actually send requests to the Etsy API. For this, we need to do the following:

  • Get Login URL from Etsy.
  • Redirect User to the Login URL.
  • After successful login, the user will be redirected to our Callback URL.
  • Process callback request to get the token from our user.
  • Request the actual access token from Etsy API

With the access token equipped, we can do any API call we’d like — requesting all open orders for example.

Preparation

We’re going to create the project and add a package called oauth.dotnetcore so we don’t have to implement the encryption, signing and so on ourselves.

dotnet new web
dotnet add package oauth.dotnetcore

In summary, we’ll create a controller called EtsyController with 2 endpoints:

  • /api/etsy/login which will create the login url and redirect the user to Etsy login URL
  • /api/etsy/callback which will be called automatically after the user logged in on the Etsy website

Afterwards, we’ll have the access token ready in our code and can request any Etsy API endpoint we want.

In our Startup.cs, we’ll make sure controllers are added and mapped.

Then, create the EtsyController.cs and let’s directly store the relevant urls and constants we’ll need. Due to our defined Route attribute and the Login function, the endpoint will be available at /api/etsy/login.

EtsyController.cs with first Login endpoint and configuration variables

We’ll neglect a few best practices for simplicity. For example, we’ll store key and secrets directly in code.

We also implement the whole login inside the controller instead of relying on a service.

Step 1: Get Login URL & Redirect user

We’ll create an endpoint Login. Inside the endpoint, we’ll create the OAuth client and set key and secret. The callback url is our backend and the callback endpoint we’ll define in the next step.

We’ll then create the request, send it and process the response. In the response, we’ll read the Login URL and the Token Secret (which will be important in the next step).

We can then directly redirect the user. This way, if the user requests our backend at /api/etsy/login, he’ll directly be redirected to the Etsy login page.

Etsy login app page after calling /api/etsy/login

Step 2: Process callback & get access token

Since we’ve defined our callback url to be /api/etsy/callback, we’ll create a Callback function. Inside, we’ll read the OAuth Token and Verifier. Using these, we’ll request the actual access token from the API.

And now we’re done! Using OAuthToken and OAuthTokenSecret, we can request any information for the authorised user.

Example call: Get orders

To round up the article, we’ll send an example request to get all open orders. We’ll use the Flurl package to make receiving the json a bit simpler.

dotnet add package flurl.http

Then, the sample request looks like this:

How to test

The full application is also available at GitHub.

Simply change the constants (consumer key, secret and request urls). Then run the application and request https://localhost:5001/api/etsy/login. It will take you to the login website on Etsy. After login, it will automatically call the callback url. You’re authorised!

If you then call api/etsy/openorders the backend will call the Etsy API, get all open orders on your account and respond with the JSON.

👋 Hi! I am Manuel Reinfurt. If you have any questions, feel free to reach out to me! You can find my social media on my website.

--

--

Manuel Reinfurt

Freelancer for Cloud (Native) Architecture & Development (mostly Azure/C#)