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

Added autoRefreshSettings config option #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MikeWillis
Copy link

I wanted my app to automatically refresh the token if necessary, instead of triggering a fatal error. So I added a config option autoRefreshSettings where you can pass an array of settings for refreshing the token. If set, a 401/invalid_token response will trigger a token refresh followed by a recursive call, so the refresh is seamless.

If the refresh is successful, the new access token and refresh tokens are stored for use on future calls. I also pass in an onSuccess handler, which you can use to save the new tokens somewhere in your DB.

I suppose you could just start every script with a call to the refresh token.. But I wanted to only run the refresh when necessary.

Example usage:

$etsy = new \Etsy\Etsy(
    $api_key,
    $access_token,
    [
        "autoRefreshSettings"=>[
            "refreshToken"=>$refreshToken, // presumably you'll have saved this in your DB somewhere
            "onSuccess"=>function($response) {
                // you'll probably want to save these in your DB
                $accessToken = $response["access_token"];
                $refreshToken = $response["refresh_token"];
            },
            "onFail"=>function($response) {
                echo "booo";
            }
        ]
    ]
);

@rhysnhall
Copy link
Owner

Hey Mike,
Thanks for the PR. I will review this as soon as I get a chance to do so.

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

Successfully merging this pull request may close these issues.

None yet

2 participants