Etsy oAuth 2.0 flow, Etsy API V3 unable to get Access Token

Viewed 334

I was able to get an authorisation code but I have problems with getting Access Token. While making a Request to get an Access Token, Every time I get Fatal error: Uncaught Exception: HTTP 404 {"error": "Resource not found"} Here is my request:

$url = "https://api.etsy.com/v3/public/oauth/token?grant_type=authorization_code&client_id={MY_ Etsy_App_ API_ Key }&redirect_uri={MY_REDIRECT_URL}&code=".$code."&code_verifier=".$code_verifier;
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'x-api-key: ={ MY_ Etsy_App_ API_ Key } ',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response_body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($status) != 200) throw new Exception("HTTP $status\n$response_body");

Where can be a mistake?

1 Answers

You're making a GET request to the token endpoint, but you should be making a POST request and sending the parameters in the request body.


curl_setopt($curl, CURLOPT_POST, true);
$headers = array(
   "Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = "grant_type=authorization_code&code=".$code."&redirect_uri="...

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

I think you don't need the x-api-key header when making a call to the token endpoint.

Related