Esty login

This tutorial will show you how to esty login, first things first, you need esty account and create new application that allows you using api. To create new one, visit this create esty application.

After that, write down client id and secret to get start with esty login.

Note: Esty login only supports OAUTH 1.0a, to find more about OAUTH 1.0a, visit https://tools.ietf.org/html/rfc5849 or short explanation here.

System requirements

  • PHP 5.4 or higher
  • APACHE 2.4.9 or higher
  • Netbean IDE

Project Structure

project structure

Implement code

Create projecthelper.php

We wrote one tutorial about projecthelper.php, view more at here, if you were already read it, please add this function in file.

  • public static function get_esty_config() {
    return array(
    'client_id' => 'client id',
    'client_secret' => 'client secret',
    'redirect_uri' => self::get_base_url('esty-callback.php'),
    'login' => self::get_base_url('esty-login.php'),
    'session' => 'tumblr_access_token'
    );
    }

Create file oauthhelper.php

We wrote one tutorial about oauthhelper.php, view more at here, if you were already read it, please add this function in file.

Create file esty-login.php

This file will undertake three main tasks, check access_token in $_SESSION, if existed, send request to get user's profile, the opposite will create $authen_url for the user grant access your applications to connect to their accounts.

- If access_token does not exsits

  • Request temporary token
  •  $method = 'GET';
    $endpoint = 'https://openapi.etsy.com/v2/oauth/request_token';
    $oauth_token = oauthhelper::get_request_token($config, $method, $endpoint);
  • Create $authen_url link from temporary token, save $oauth_token and $oauth_token_secret in $_SESSION
  •  $_SESSION['oauth_token'] = $oauth_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $oauth_token['oauth_token_secret'];
    $authen_url = 'https://www.etsy.com/oauth/signin' . '?oauth_token=' . $oauth_token['oauth_token'];
    echo '<a href="' . $authen_url . '">Login with Esty</a>';

- Send request to get user's profile and print result on screen if access_token exists

  •  $access_token = $_SESSION[$config['session']];
    $endpoint = 'https://openapi.etsy.com/v2/users/__SELF__';
    $user = oauthhelper::get_info($config, $endpoint, $access_token);
    var_dump($user);

<?php

session_start();
include 'projecthelper.php';
include './oauthhelper.php';

$config = projecthelper::get_esty_config();
if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
$endpoint = 'https://openapi.etsy.com/v2/users/__SELF__';
$user = oauthhelper::get_info($config, $endpoint, $access_token);
var_dump($user);
} else {
$method = 'GET';
$endpoint = 'https://openapi.etsy.com/v2/oauth/request_token';
$oauth_token = oauthhelper::get_request_token($config, $method, $endpoint);

if (isset($oauth_token['oauth_token'])) {
$_SESSION['oauth_token'] = $oauth_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $oauth_token['oauth_token_secret'];
$authen_url = 'https://www.etsy.com/oauth/signin' . '?oauth_token=' . $oauth_token['oauth_token'];
echo '<a href="' . $authen_url . '">Login with Esty</a>';
} else {
print_r($oauth_token);
}
}







die;



if (isset($_SESSION[$config['session']])) {
$accessToken = $_SESSION[$config['session']];
// oauth with access token
$connection = new TwitterOAuth($config['client_id'], $base_url, $config['client_secret'], $accessToken['oauth_token'], $accessToken['oauth_token_secret']);

$user = (array) $connection->get("users/__SELF__", array(), 'https://openapi.etsy.com/v2/users/__SELF__');
var_dump($user);
} else {
$connection = new TwitterOAuth($config['client_id'], $config['client_secret'], $base_url);
// make request token
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => $config['redirect_uri']));
// save oauth_token and oauth_token_secret to session, we'll use later in twitter-callback.php
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
var_dump($request_token);
$url = 'https://www.etsy.com/oauth/signin?oauth_token=' . $request_token['oauth_token'] . '&oauth_consumer_key=' . $request_token['oauth_consumer_key'];
echo '<a href="' . $url . '">Login with Esty</a>';
}

Create file esty-callback.php

After granted access to your application, user will be redirected to your redirect_uri with authentication code. Then you will make request to get access_token.
Note: In some cases, your redirect_uri that you provide must match with the one  you've described in app's settings, or redirect_uri must begin with https

- Check and extract from url oauth_verifier

  • $oauth_verifier = $_GET['oauth_verifier'];

Get oauth_token and oauth_token_secret from $_SESSION

  • $temp_token['oauth_token'] = $_SESSION['oauth_token'];
    $temp_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

- Make request to get access_token 

  •  $endpoint = 'https://openapi.etsy.com/v2/oauth/access_token';
    $method = "GET";
    $access_token = oauthhelper::get_access_token($config, $method, $endpoint, $temp_token, $oauth_verifier);

- Check return data, if exists access_token, save token in $_SESSION and redirect to login page

  •  $_SESSION[$config['session']] = $access_token;
    header('Location: ' . $config['login']);

<?php

session_start();
include 'projecthelper.php';
include 'projecthelper.php';

$config = projecthelper::get_esty_config();

if (isset($_GET['oauth_verifier'])) {
$oauth_verifier = $_GET['oauth_verifier'];
$temp_token['oauth_token'] = $_SESSION['oauth_token'];
$temp_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

$endpoint = 'https://openapi.etsy.com/v2/oauth/access_token';
$method = "GET";
$access_token = oauthhelper::get_access_token($config, $method, $endpoint, $temp_token, $oauth_verifier);

if (is_array($access_token) && isset($access_token['oauth_token'])) {
$_SESSION[$config['session']] = $access_token;
header('Location: ' . $config['login']);
} else {
var_dump($access_token);
}
} else {
print_r($_GET);
}

Run project and view result, or view example at here. If you have questions, don't hesitate, tell us