Request an access and a refresh token from the HubiC API.

        Those tokens are mandatory and will be used for subsequent file
        operations. They are not returned and will be stored internaly.

        :param keystoneclient.Session session: The session object to use for
                                               queries.

        :raises keystoneclient.exceptions.AuthorizationFailure: if something
                                                                goes wrong.

def _refresh_tokens(self, session):
        """Request an access and a refresh token from the HubiC API.

        Those tokens are mandatory and will be used for subsequent file
        operations. They are not returned and will be stored internaly.

        :param keystoneclient.Session session: The session object to use for
                                               queries.

        :raises keystoneclient.exceptions.AuthorizationFailure: if something
                                                                goes wrong.
        """

        params = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
        }

        payload = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
        }

        if self.refresh_token is None:
            # if we don't have a refresh token, we need an authorization token
            # first
            payload['grant_type']   = 'authorization_code'
            payload['code']         = self._get_authorization_token(session)
            payload['redirect_uri'] = self.redirect_uri
        else:
            # when we have a refresh token, we DON'T need an authorization
            # token to request a new one
            payload['grant_type']    = 'refresh_token'
            payload['refresh_token'] = self.refresh_token

        r = session.post("https://api.hubic.com/oauth/token",
                         params=params,
                         data=payload,
                         authenticated=False)
        if r.status_code != 200 and self.refresh_token is not None:
            # if we had a refresh token, try again without it
            # (might be expired)
            payload['grant_type']   = 'authorization_code'
            payload['code']         = self._get_authorization_token(session)
            payload['redirect_uri'] = self.redirect_uri
            r = session.post("https://api.hubic.com/oauth/token",
                             params=params,
                             data=payload,
                             authenticated=False)

        if r.status_code != 200:
            raise AuthorizationFailure()

        response = r.json()
        if 'error' in response:
            raise AuthorizationFailure()

        self.access_token = response['access_token']

        # refresh_token entry will not be there is we are just refreshing an
        # old token.
        if 'refresh_token' in response:
            self.refresh_token = response['refresh_token']