def test_get_refresh_token(self):
        self.freshBooksClient = FreshBooksClient(
            client_id="some_client",
            client_secret="some_secret",
            redirect_uri="https://example.com",
            access_token="an_old_token",
            refresh_token="an_old_refresh_token")
        url = "{}/auth/oauth/token".format(API_BASE_URL)
        httpretty.register_uri(httpretty.POST,
                               url,
                               body=json.dumps(
                                   get_fixture("auth_token_response")),
                               status=200)

        result = self.freshBooksClient.refresh_access_token()

        assert httpretty.last_request().body == (
            "client_id=some_client&client_secret=some_secret&grant_type=refresh_token"
            "&redirect_uri=https%3A%2F%2Fexample.com&refresh_token=an_old_refresh_token"
        ).encode("utf-8")
        assert self.freshBooksClient.access_token == "my_access_token"
        assert result.access_token == "my_access_token"
        assert self.freshBooksClient.refresh_token == "my_refresh_token"
        assert result.refresh_token == "my_refresh_token"
        assert self.freshBooksClient.access_token_expires_at == datetime(
            2010, 10, 17)
        assert result.access_token_expires_at == datetime(2010, 10, 17)
    def test_list_clients(self):
        freshBooksClient = FreshBooksClient(client_id="some_client",
                                            access_token="some_token",
                                            user_agent="phone_home")
        client_ids = [12345, 12346, 12457]
        url = "{}/accounting/account/{}/users/clients".format(
            API_BASE_URL, self.account_id)
        httpretty.register_uri(httpretty.GET,
                               url,
                               body=json.dumps(
                                   get_fixture("list_clients_response")),
                               status=200)

        clients = freshBooksClient.clients.list(self.account_id)

        assert str(clients) == "ListResult(clients)"
        assert len(clients) == 3
        assert clients.pages.total == 3
        assert clients.data["total"] == 3
        assert clients[0].userid == client_ids[0]
        assert clients.data["clients"][0]["userid"] == client_ids[0]
        for index, client in enumerate(clients):
            assert client.userid == client_ids[index]
        assert httpretty.last_request(
        ).headers["Authorization"] == "Bearer some_token"
        assert httpretty.last_request().headers["Content-Type"] is None
        assert httpretty.last_request().headers["user-agent"] == "phone_home"
Esempio n. 3
0
 def setup_method(self, method):
     self.business_id = 98765
     self.freshBooksClient = FreshBooksClient(client_id="some_client",
                                              access_token="some_token")
 def setup_method(self, method):
     self.account_id = "ACM123"
     self.freshBooksClient = FreshBooksClient(client_id="some_client",
                                              access_token="some_token")
Esempio n. 5
0
# This is an example where we create a new client and an invoice for them.

from datetime import date
from freshbooks import Client as FreshBooksClient
from freshbooks import FreshBooksError

fb_client_id = "<your client id>"
access_token = "<your access token>"
account_id = "<your account id>"

freshBooksClient = FreshBooksClient(client_id=fb_client_id, access_token=access_token)

# Create the client
print("Creating client...")
try:
    client_data = {"organization": "Python SDK Test Client"}
    client = freshBooksClient.clients.create(account_id, client_data)
except FreshBooksError as e:
    print(e)
    print(e.status_code)
    exit(1)

print(f"Created client {client.id}")

# Create the invoice
line1 = {
    "name": "Fancy Dishes",
    "description": "They're pretty swanky",
    "qty": 6,
    "unit_cost": {
        "amount": "27.00",
 def test_get_access_token__redirect_not_provided(self):
     freshBooksClient = FreshBooksClient(client_id="some_client",
                                         client_secret="some_secret")
     with pytest.raises(FreshBooksClientConfigError):
         freshBooksClient.get_access_token("some_grant")
 def test_get_access_token__secret_not_provided(self):
     freshBooksClient = FreshBooksClient(client_id="some_client",
                                         redirect_uri="https://example.com")
     with pytest.raises(FreshBooksClientConfigError):
         freshBooksClient.get_access_token("some_grant")
 def test_get_auth_request_url__redirect_not_provided(self):
     freshBooksClient = FreshBooksClient(client_id="some_client",
                                         client_secret="some_secret")
     with pytest.raises(FreshBooksClientConfigError):
         freshBooksClient.get_auth_request_url()
 def setup_method(self, method):
     self.freshBooksClient = FreshBooksClient(
         client_id="some_client",
         client_secret="some_secret",
         redirect_uri="https://example.com")
Esempio n. 10
0
# This is an example where we run through the OAuth flow,
# select a business, and display a client from that business.

from types import SimpleNamespace
from freshbooks import Client as FreshBooksClient

fb_client_id = "<your client id>"
secret = "<your client secret>"
redirect_uri = "<your redirect uri>"

freshBooksClient = FreshBooksClient(client_id=fb_client_id,
                                    client_secret=secret,
                                    redirect_uri=redirect_uri)

authorization_url = freshBooksClient.get_auth_request_url(
    scopes=['user:profile:read', 'user:clients:read'])
print(f"Go to this URL to authorize: {authorization_url}")

# Going to that URL will prompt the user to log into FreshBooks and authorize the application.
# Once authorized, FreshBooks will redirect the user to your `redirect_uri` with the authorization
# code will be a parameter in the URL.
auth_code = input("Enter the code you get after authorization: ")

# This will exchange the authorization code for an access token
token_response = freshBooksClient.get_access_token(auth_code)
print(
    f"This is the access token the client is now configurated with: {token_response.access_token}"
)
print(f"It is good until {token_response.access_token_expires_at}")
print()