XF 2.2 Any work with providers and refresh tokens?

robdog

Well-known member
I am attempting to work with a data provider that requires the use of a refresh token. I really wanted to use the provided vendor code, but just can't seem to get it working correctly. I am having a hard time with the function refreshAccessToken. Once I use that function, the saved token object no longer has a refresh token. I am assuming I will need the refresh token to make more access tokens, but it keeps getting cleared during that function call.

Anyone else go through this process before? Any info would be helpful, thanks.
 
I continued some of my work and here is the function that I created/updated:

PHP:
    public function refreshAccessToken(TokenInterface $token)
    {
        $refreshToken = $token->getRefreshToken();
        if (empty($refreshToken)) {
            throw new MissingRefreshTokenException();
        }

        $parameters = array(
            'grant_type'    => 'refresh_token',
            'access_type'   => 'offline',
            'type'          => 'web_server',
            'client_id'     => $this->credentials->getConsumerId(),
            'client_secret' => $this->credentials->getConsumerSecret(),
            'refresh_token' => $refreshToken,
        );

        $responseBody = $this->httpClient->retrieveResponse(
            $this->getAccessTokenEndpoint(),
            $parameters,
            $this->getExtraOAuthHeaders()
        );
        // Parsing the token response.
        $token = $this->parseAccessTokenResponse($responseBody);

        // Getting the current token value.
        $currentToken = $this->storage->retrieveAccessToken($this->service());
        // Setting the new access token and end of life values.
        $currentToken->setAccessToken($token->getAccessToken());
        $currentToken->setEndOfLife($token->getEndOfLife());

        // Updating the current token value in the store.
        $this->storage->storeAccessToken($this->service(), $currentToken);
    }

Basically, I had to override the refreshAccessToken function in the provider service file I created. In this function, everything is pretty much the same, but I get the current token and set values from the new token I requested. The reason for this is because I want to make sure the refresh token is not overridden so we can use it again. So on the current token I am just updating the access token and the end of life value. Then saving that back down to the store for later use.

Hope this helps someone or if you have feedback on a better implementation, let me know!
 
Top Bottom