Having failed miserably using C# code to get an Etsy OAuth 2 token, I resorted to using Postman for the initial request figuring that it would then be a simple matter of requesting a refresh token as and when needed. So, from Postman, all good, I can sign in and grant access, which then causes Etsy to respond with a shortlived access token (and a refresh token to use to request another access token without having to go through the whole grant process again).
However, I cannot figure out how to get another access token using this refresh token as Etsy keeps on responding with grant_type is required.
OK, the initial response from Postman following the success grant access is:
{
"access_token": "<initial-access-token-from_Etsy>",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "<my-refresh-token>"
}
And following their docs to request a refresh OAuth token, it implies that all I need to do is to make a POST request to https://openapi.etsy.com/v3/public/oauth/token, adding the following parameters to the JSON body with content type: application/x-www-form-urlencoded:
{
"grant_type": "refresh_token",
"client_id": "<my-client-API-string>",
"refresh_token": "<my-refresh-token>"
}
Thus, setting all this up in Postman and looking at the generated code for the request gives:
var client = new RestClient("https://openapi.etsy.com/v3/public/oauth/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("x-api-key", "<my-client-API-string>");*
request.AddParameter("grant_type", "refresh_token");
request.AddParameter("client_id", "<my-client-API-string>");
request.AddParameter("refresh_token", "<my-refresh-token>");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
*NOTE - I had to add in the header the parameter x-api-key otherwise it would fail with "error": "Missing x-api-key header or client_id"
Alas, the response from Etsy is always
{
"error": "invalid_request",
"error_description": "grant_type is required"
}
This post guided me in making the initial request.
Why is it moaning about grant_type when it's in the body as requested!?!?!?