Etsy integration v2 with Symfony
July 12, 2021
July 12, 2021

public function connectEtsy(){
$oauth = new \OAuth(consumer_key, shared_secret);
$req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r%20listings_w%20transactions_r%20transactions_w",return_url, "GET");
$customer = $this->getUser()->getCustomer();
$customer->setEtsyOauthSecret($req_token['oauth_token_secret']);
$this->em->flush();
return $this->getResponse($req_token['login_url']);
}
What we have here is a function on our backend that we will call when our user wishes to connect to our application, when we send our request to Etsy, if our request is ok, we should get a link to send the user to, and that link should be to Etsy’s website where the user essentially agrees to gives us tokens with which we can access editing stuff on their store/account without them giving us any credentials.
So first thing that you will have to change from this piece of code are the arguments sent to the Oauth function, here is where the registration of the application in previous steps comes in, the keystring is the consumer key and the shared secret is the consumer secret.
The next part that needs to be addressed is the first parameter in the getRequestToken, here you can see in the url that there are scopes, essentially what these do is they give you access to different parts of the users account, so for example if you send listings_w and listings_r, that means that you can read and write listings to and from the users account, I would suggest taking a bit of time to read and consider which scopes you will need, because the user does see this when he is accepting to give you the tokens on the Etsy website.
The third part which needs to be changed is the return_url, here you should add an url from your website where you wish Etsy to return your user after they have accepted giving you access to their account.
Once the user has accepted to give you access and they are returned to the return_url that you put as an argument, you will have in that url additional parameters that etsy sent you, those parameters are oauth_token and oauth_verifier, now with these we are one step behind getting access to the users account.
$oauth_token = $this->getParameter('oauth_token');
$oauth_verifier = $this->getParameter('oauth_verifier');
$customer = $this->getUser()->getCustomer();
$request_token_secret = $customer->getEtsyOauthSecret();
// get the verifier from the url
$oauth = new \OAuth(consumer_key, consumer_secret);
// set the temporary credentials and secret
$oauth->setToken($oauth_token, $request_token_secret);
try {
// set the verifier and request Etsy's token credentials url
$acc_token = $oauth->getAccessToken("https://openapi.etsy.com/v2/oauth/access_token", null, $oauth_verifier, "GET");
} catch (\OAuthException $e) {
error_log($e->getMessage());
}
$customer->setEtsyOauthAccessToken($acc_token['oauth_token']);
$customer->setEtsyOauthAccessTokenSecret($acc_token['oauth_token_secret']);
Now with the temporary credentials we got from the previous code snippet and which we saved to our database, here we use them to get permanent credentials from Etsy for our customer, that is the access token and the access token secret, with these two and the adequate scopes we requested, we can do almost anything with the users account and store.