I’m attempting to connect to Etsy using API v3, but I’m stuck and unsure how to proceed. Despite trying ChatGPT, forums, and other resources, I haven’t had any success in solving my issue.
Here is my script:
<?php
session_start();
$client_id = 'my cllient id';
$redirect_uri = 'https://www.mywebsite.eu/etsy';
$scope = 'email_r';
function base64url_encode($data) {
return rtrim(strtr(base64_encode(utf8_encode($data)), '+/', '-_'), '=');
}
function base64url_decode($data) {
return utf8_decode(base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)));
}
function sha256($data) {
return hash('sha256', $data);
}
if(isset($_SESSION['state'])) {
echo 'State: ' . $_SESSION['state'] . '<br>';
} else {
echo 'The state not setup<br>';
}
if(isset($_GET['code'])) {
if(isset($_SESSION['state']) && $_GET['state'] === $_SESSION['state']) {
$code_verifier = random_bytes(32);
echo '1) $code_verifier: ' . bin2hex($code_verifier) . '<br>';
$code_challenge = base64url_encode(sha256($code_verifier));
echo '2) $code_challenge: ' . $code_challenge . '<br>';
$encoded_code_verifier = base64url_encode($code_verifier);
echo 'Encoded code_verifier: ' . $encoded_code_verifier . '<br>';
$decoded_code_verifier = base64url_decode($encoded_code_verifier);
echo 'Decoded code_verifier: ' . bin2hex($decoded_code_verifier) . '<br>';
$token_request_data = array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => 'my_secret',
'code_verifier' => $encoded_code_verifier
);
echo "$token_request_data array: <br>";
var_dump($token_request_data);
$token_url = 'https://api.etsy.com/v3/public/oauth/token';
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_request_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo 'CURL response: ' . $response . '<br>';
if($response === false) {
echo 'Query error: ' . curl_error($ch);
} else {
$token_data = json_decode($response, true);
$access_token = $token_data['access_token'];
echo '$response: ' . $response . '<br>';
echo '$access_token:';
var_dump($access_token);
}
} else {
echo 'Not correct session state';
}
} else {
$_SESSION['state'] = bin2hex(random_bytes(16));
$code_verifier = random_bytes(32);
$code_challenge = base64url_encode(sha256($code_verifier));
$authorization_url = 'https://www.etsy.com/oauth/connect' .
'?response_type=code' .
'&redirect_uri=' . urlencode($redirect_uri) .
'&scope=' . $scope .
'&client_id=' . $client_id .
'&state=' . $_SESSION['state'] .
'&code_challenge=' . $code_challenge . /
'&code_challenge_method=S256';
echo 'Authorization URL: ' . $authorization_url . '<br>';
header('Location: ' . $authorization_url);
exit;
}
?>
Output:
State: eb448883c776b7daa2b1a829a56200b3 1) $code_verifier:
ea9557f088a2767dd399d8c6d5446011a2af524f8a2180f4731744a240f7afa2 2)
$code_challenge:
MTRmYjM1MWM5ZGFmMTZkMWRkZDNjNGZkYjFhYmU3OGE3MTFmZDZmODYzM2EwMTI4MTY1NmZjMmYzMGQ2MTNhYg
Encoded code_verifier:
w6rClVfDsMKIwqJ2fcOTwpnDmMOGw5VEYBHCosKvUk_CiiHCgMO0cxdEwqJAw7fCr8Ki
Decoded code_verifier:
ea9557f088a2767dd399d8c6d5446011a2af524f8a2180f4731744a240f7afa2`Error: `CURL response:
{“error”:”invalid_grant”,”error_description”:”code_verifier is
invalid”} $access_token:NULL
I have tried ChatGPT, forums, the Etsy manual, the OAuth web page, and other resources. ChatGPT said my PHP script is okay and it cannot help me further. My PHP skills are not high enough to solve my problem. I am expecting to get Etsy access token.