Web service with ETSY API

129 views Asked by At

I want to write a web service for myself using ETSY API (with ASP.net CORE API).

I have no idea how to use the ETSY API.

For example, I want to list the products and stores that have opened and sold the most in the last 2 months.

I have 2-3 simple queries in mind. How should I proceed?

First of all, I need to pass the Authentication part, etc. . Do you have an example? It can also be done with different programming languages, ultimately it will be enough to answer 2-3 queries.

Please reach out to anyone I can contact who can support me...

I received my KEYSTRING and SHARED SECRET values ​​from the ETSY API side. I guess it wasn't active.

I've been waiting for 3 days. Does anyone have any information about the length of this process?

1

There are 1 answers

0
Jalpesh Vadgama On

I think you should read their documentation. There is great documentation for esty apis. You can find that here https://www.etsy.com/developers/documentation/getting_started/api_basics

Apart from that there is a great article from Manuel Reinfurt.You can find that here - https://manuelreinfurt.medium.com/use-etsy-api-with-oauth-1-0-in-asp-net-core-c-819fb6edd376

And entire source code is will found on here -https://github.com/mreinfurt/etsy-aspnetcore

Basically, you need to do the following

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

Etsy uses open API authentication. For that you can use this nugget package here-https://github.com/rhargreaves/oauth-dotnetcore

Write the code as mentioned in the article. Write startup file like below.

public class Startup
{
   public void ConfigureServices(IServiceCollection services)
   {
      services.AddControllers();
   }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }

      app.UseRouting();
      app.UseEndpoints(endpoints =>
      {
          endpoints.MapControllers();
      });
  }
}

Then in controller create properties like below

[Route("api/[controller]/[action]")]
[ApiController]
public class EtsyController : ControllerBase
{
    private const string RequestUrl = 
     "https://openapi.etsy.com/v2/oauth/request_token";
   private const string RequestAccessTokenUrl = 
     "https://openapi.etsy.com/v2/oauth/access_token";

   private const string ConsumerKey = "YOUR_CONSUMER_KEY";
    private const string ConsumerSecret = "YOUR_CONSUMER_SECRET";
}

Then for login and redirection you need to write following code

 private static string TokenSecret { get; set; }

[HttpGet]
public IActionResult Login()
{
   // Configure our OAuth client
   var client = new OAuthRequest
   {
      Method = "GET",
      Type = OAuthRequestType.RequestToken,
      SignatureMethod = OAuthSignatureMethod.HmacSha1,
      ConsumerKey = ConsumerKey,
      ConsumerSecret = ConsumerSecret,
      RequestUrl = RequestUrl,
       CallbackUrl = "https://localhost:5001/api/etsy/callback"
   };

    // Build request url and send the request
   var url = client.RequestUrl + "?" + client.GetAuthorizationQuery();
   var request = (HttpWebRequest)WebRequest.Create(url);
   var response = (HttpWebResponse)request.GetResponse();

   using var dataStream = response.GetResponseStream();
   var reader = new StreamReader(dataStream);
   var responseFromServer = reader.ReadToEnd();

   // Parse login_url and oauth_token_secret from response
  var loginUrl = 
     HttpUtility.ParseQueryString(responseFromServer).Get("login_url");
   TokenSecret = HttpUtility.ParseQueryString(responseFromServer).Get("oauth_token_secret");

   // User will automatically be redirected to the Etsy login website
   return Redirect(loginUrl);
}

Then process and call back code like following.

private static string OAuthToken { get; set; }
private static string OAuthTokenSecret { get; set; }

 [HttpGet]
public IActionResult Callback()
{
   // Read token and verifier
  string token = Request.Query["oauth_token"];
  string verifier = Request.Query["oauth_verifier"];

  // Create access token request
  var client = new OAuthRequest
  {
    Method = "GET",
    Type = OAuthRequestType.RequestToken,
    SignatureMethod = OAuthSignatureMethod.HmacSha1,
    ConsumerKey = ConsumerKey,
    ConsumerSecret = ConsumerSecret,
    Token = token,
    TokenSecret = TokenSecret,
    RequestUrl = RequestAccessTokenUrl,
    Verifier = verifier
};

// Build request url and send the request
var url = client.RequestUrl + "?" + client.GetAuthorizationQuery();
var request = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();

using var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var responseFromServer = reader.ReadToEnd();

// Parse and save access token and secret
OAuthToken = HttpUtility.ParseQueryString(responseFromServer).Get("oauth_token");
OAuthTokenSecret = HttpUtility.ParseQueryString(responseFromServer).Get("oauth_token_secret");

return Ok();
 }

Then get the order from an Etsy account like the following.

[HttpGet]

public async Task OpenOrders() { const string requestUrl = "https://openapi.etsy.com/v2/shops/YOURSHOPNAME/receipts/open?";

var client = new OAuthRequest
{
    Method = "GET",
    Type = OAuthRequestType.ProtectedResource,
    SignatureMethod = OAuthSignatureMethod.HmacSha1,
    ConsumerKey = ConsumerKey,
    ConsumerSecret = ConsumerSecret,
    Token = OAuthToken,
    TokenSecret = OAuthTokenSecret,
    RequestUrl = requestUrl,
};

var url = requestUrl + client.GetAuthorizationQuery();
var result = await url.GetStringAsync();
return Content(result, "application/json");
   }

You can get more ideas from the link shared at the beginning!