Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

Etsy Api PHP Authentified request

Currently I want to make a page with a button that will retrieves a list of all my shop orders on ETSY.

I found the perfect package https://github.com/inakiabt/etsy-php With it, once I retrieved access token, I'll just have to do something like this

$api->findAllShopReceipts(array('params' => array('shop_id' => '__SELF__')))

But my Xampp (I am on win 10 x64) don't have OAuth (phpinfo()) so I had to use this package : https://github.com/Y0lk/oauth1-etsy

Here's my code :

class EtsyController extends MasterController { const VIEWS = 'etsyapp';

private $client;

/**
 * Constructeur
 */
public function __construct()
{
    parent::__construct();
    
    $this->client = new Etsy([
        'identifier'            =>      '***********************',
        'secret'                =>      '**********',
        'scope'                 =>      'listings_r transactions_r',
        'callback_uri'          =>      'http://**********.loc/etsyapp/next',
        'access_token'          =>      $_SESSION['access_token'],
        'access_token_secret'   =>      $_SESSION['access_token_secret'],
    ]);
}


/**
 * TEST AVEC PACKAGE Y0lk
 */
public function EtsyConnect()
{

    
    
    // On envoi une requête à l'API avec notre consumer & secrey key, et elle nous renvoi nos identifiants temporaires 
    $tempCred = $this->client->getTemporaryCredentials();

    // On store nos identifiants dans la Session
    $_SESSION['temporary_credentials'] = serialize($tempCred);

    $tmpSecret = $tempCred->getSecret();
    $tmpId = $tempCred->getIdentifier();
    
    // Avec nos infos on creer un lien vers une page d'autoriation de liaison de compte Etsy avec notre API
    $redirect_url = $this->client->getAuthorizationUrl($tempCred);
   
   return $this->render(self::VIEWS.'/home', [
        'redirect_url'  =>  $redirect_url,
   ]);
}


/**
 * TEST AVEC PACKAGE Y0lk
 */
public function EtsyNext()
{
    
    if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
        // Retrieve the temporary credentials we saved before
        $tempCred = unserialize($_SESSION['temporary_credentials']);
    
        // We will now retrieve token credentials from the server
        $tokenCredentials = $this->client->getTokenCredentials($tempCred, $_GET['oauth_token'], $_GET['oauth_verifier']);
        
        // Je stock mes token en BDD (ou en SESSION, ou ailleurs...)
        $_SESSION['all_access_token'] = $tokenCredentials;
        $_SESSION['access_token'] = $tokenCredentials->getIdentifier();
        $_SESSION['access_token_secret'] = $tokenCredentials->getSecret();
    }

   return $this->render(self::VIEWS.'/next', []);
}

}

I used EtsyConnect to get temp credentials, that allow me to go to etsy login/allow access page, and when confirmed, callback on my uri with my access token in the URL, in EtsyNext I get these access tokens, and store them in DB (here for my test I just put them in SESSION). Now that my $this->client = new Etsy have all tokens, It mean I won't have to go to the login page again.

But from here, how do I perform this request :

enter image description here

I know that with inakiabt package, all I'll have to do from here is something like this :

$api = new EtsyApi($this->client);
$api->findAllShopReceipts(array('params' => array('shop_id' => '__SELF__')));

But without it I really don't know. I even tried to call the url with curl but normal answer : This method requires authentication.

Can someone help me to do this authentified request?

question from:https://stackoverflow.com/questions/66065096/etsy-api-php-authentified-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...