Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem during the callback #3

Open
Niji-Bemani opened this issue Feb 28, 2019 · 1 comment
Open

Problem during the callback #3

Niji-Bemani opened this issue Feb 28, 2019 · 1 comment

Comments

@Niji-Bemani
Copy link

Niji-Bemani commented Feb 28, 2019

Hi,
I tried to use this package, but I have some troubles to make it work.

Firstly, I got the same issue discussed in this pull request. So, I used this fix and I could get the authorization url from Etsy. (thanks to Joe Dawson!)

For the next, I don't know is the good way to do, but I made a redirection to this authorization url to be able to click on the button to allow access to my app.

Then, I'm getting the following error during the callback :

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Argument 1 passed to League\OAuth1\Client\Server\Server::getTokenCredentials() must be an instance of League\OAuth1\Client\Credentials\TemporaryCredentials, boolean given, called in /vendor/gentor/etsy-php-laravel/src/EtsyService.php on line 79

Actually, when I check the approve function in EtsyService.php, I can find unserialize($this->session->get('temporary_credentials')); returns false

public function approve($token, $verifier)
    {
        // Retrieve the temporary credentials we saved before
        $temporaryCredentials = unserialize($this->session->get('temporary_credentials'));

        return $this->server->getTokenCredentials($temporaryCredentials, $token, $verifier);
    }

This is my controller :

<?php

namespace App\Http\Controllers;

use Gentor\Etsy\Facades\Etsy;
use Illuminate\Http\Request;

class EtsyController extends Controller
{
    public function index()
    {
        // The $callbackUrl is the url of your app where Etsy sends the data needed for getting token credentials
        $callbackUrl = 'http://127.0.0.1:8001/etsy/callback';

        // The $authorizationUrl is the Etsy url where the user approves your app
        $authorizationUrl = Etsy::authorize($callbackUrl);

        return redirect($authorizationUrl);
    }

    public function store(Request $request)
    {
        // On the callback endpoint run this code to get the token credentials and add them to your config
        $tokenCredentials = Etsy::approve($request->get('oauth_token'), $request->get('oauth_verifier'));

        $tokens = [
            'access_token' => $tokenCredentials->getIdentifier(),
            'access_token_secret' => $tokenCredentials->getSecret(),
        ];

        dd($tokens);
    }
}

It seems that is a session problem, maybe I should not make a redirection, but I don't know what else to do.

@Niji-Bemani
Copy link
Author

Hi,
finally I found a way to make it works for me, by using Session instead of SessionManager.

I would like to make a pull request but have no idea how to do that (I'm still a begginer with git, sorry). Please find my modifications below, hope this can help someone else.

/src/EtsyService.php

<?php

namespace Gentor\Etsy;


use Gentor\OAuth1Etsy\Client\Server\Etsy;
use League\OAuth1\Client\Credentials\TokenCredentials;
use Illuminate\Support\Facades\Session;

/**
 * Class EtsyService
 * @package Gentor\Etsy
 */
class EtsyService
{
    /** @var Etsy $server */
    private $server;

    /** @var TokenCredentials $tokenCredentials */
    private $tokenCredentials;

    /**
     * EtsyService constructor.
     * @param SessionManager $session
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->server = new Etsy([
            'identifier' => $config['consumer_key'],
            'secret' => $config['consumer_secret'],
            'scope' => !empty($config['scope']) ? $config['scope'] : '',
            'callback_uri' => ''
        ]);

        if (!empty($config['access_token']) && !empty($config['access_token_secret'])) {
            $tokenCredentials = new TokenCredentials();
            $tokenCredentials->setIdentifier($config['access_token']);
            $tokenCredentials->setSecret($config['access_token_secret']);

            $this->tokenCredentials = $tokenCredentials;
        }
    }

    /**
     * @param $callbackUri
     * @return string
     */
    public function authorize($callbackUri)
    {
        $this->server->getClientCredentials()->setCallbackUri($callbackUri);

        // Retrieve temporary credentials
        $temporaryCredentials = $this->server->getTemporaryCredentials();

        // Store credentials in the session, we'll need them later
        Session::put('temporary_credentials', $temporaryCredentials);

        return $this->server->getAuthorizationUrl($temporaryCredentials);
    }

    /**
     * @param $token
     * @param $verifier
     * @return \League\OAuth1\Client\Credentials\TokenCredentials
     */
    public function approve($token, $verifier)
    {
        // Retrieve the temporary credentials we saved before
        $temporaryCredentials = Session::get('temporary_credentials');

        return $this->server->getTokenCredentials($temporaryCredentials, $token, $verifier);
    }

    /**
     * Get the user's unique identifier (primary key).
     *
     * @param bool $force
     *
     * @return string|int
     */
    public function getUserUid($force = false)
    {
        $userDetails = $this->getUserDetails($force);

        return $userDetails->uid;
    }

    /**
     * Get user details by providing valid token credentials.
     *
     * @param bool $force
     *
     * @return \League\OAuth1\Client\Server\User
     */
    public function getUserDetails($force = false)
    {
        return $this->server->getUserDetails($this->tokenCredentials, $force);
    }

    /**
     * @param $method
     * @param array $args
     * @return array
     */
    public function __call($method, array $args)
    {
        $api = new EtsyApi($this->server, $this->tokenCredentials);

        return call_user_func_array([$api, $method], $args);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant