def revoke(self, token=None):
        """Revokes access to QBO company/User Info using either valid Refresh Token or Access Token

        :param token: Refresh Token or Access Token to revoke
        :raises ValueError: if Refresh Token or Access Token value not specified
        :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
        :return: True if token successfully revoked
        """

        token_to_revoke = token or self.refresh_token or self.access_token
        if token_to_revoke is None:
            raise ValueError('Token to revoke not specified')

        headers = {
            'Content-Type': 'application/json',
            'Authorization': get_auth_header(self.client_id,
                                             self.client_secret)
        }

        body = {'token': token_to_revoke}

        send_request('POST',
                     self.revoke_endpoint,
                     headers,
                     self,
                     body=json.dumps(body),
                     session=self)
        return True
    def get_bearer_token(self, auth_code, realm_id=None):
        """Gets access_token and refresh_token using authorization code
        
        :param auth_code: Authorization code received from redirect_uri
        :param realm_id: Realm ID/Company ID of the QBO company
        :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
        """

        realm = realm_id or self.realm_id
        if realm is not None:
            self.realm_id = realm

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': get_auth_header(self.client_id,
                                             self.client_secret)
        }

        body = {
            'grant_type': 'authorization_code',
            'code': auth_code,
            'redirect_uri': self.redirect_uri
        }

        send_request('POST',
                     self.token_endpoint,
                     headers,
                     self,
                     body=urlencode(body),
                     session=self)
    def refresh(self, refresh_token=None):
        """Gets fresh access_token and refresh_token 
        
        :param refresh_token: Refresh Token
        :raises ValueError: if Refresh Token value not specified
        :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
        """

        token = refresh_token or self.refresh_token
        if token is None:
            raise ValueError('Refresh token not specified')

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': get_auth_header(self.client_id,
                                             self.client_secret)
        }

        body = {'grant_type': 'refresh_token', 'refresh_token': token}

        send_request('POST',
                     self.token_endpoint,
                     headers,
                     self,
                     body=urlencode(body),
                     session=self)
Beispiel #4
0
    def refresh(self, refresh_token=None):
        """Gets fresh access_token and refresh_token 
        
        :param refresh_token: Refresh Token

        :returns: True if token was successfully refreshed, False otherwise

        :raises ValueError: if Refresh Token value not specified
        :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
        """

        token = refresh_token or self.refresh_token
        if token is None:
            raise ValueError('Refresh token not specified')

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': get_auth_header(self.client_id,
                                             self.client_secret)
        }

        body = {'grant_type': 'refresh_token', 'refresh_token': token}

        response = send_request('POST',
                                self.token_endpoint,
                                headers,
                                self,
                                body=urlencode(body),
                                session=self)
        response.raise_for_status()

        if response.status_code == 200:
            data = json.loads(response.text)

            self.access_token = data["access_token"]
            self.expires_in = data["expires_in"]
            self.refresh_token = data["refresh_token"]
            self.x_refresh_token_expires_in = data[
                "x_refresh_token_expires_in"]
            return True

        return False