Authenticating to FreshBooks
FreshBooks uses the OAuth authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See Using OAuth Authentication for an authentication guide.
Using OAuth Authentication
OAuth requires the authenticating user to interact with FreshBooks using the browser. The provider facilitates this in various ways, as described below.
Custom Credentials
You will need to register an app to obtain the OAuthClientId and OAuthClientSecret.
Web Applications
You need to create an OAuth app to use in the web flow.
Desktop Applications
You need to create an OAuth app.
Headless Machines
In the headless OAuth flow, users need to authenticate via a browser on another machine. You will need to create a custom OAuth app.
Creating a Custom OAuth App
See Creating a Custom OAuth App for the procedure.
Custom Credentials
Desktop Authentication with your OAuth App
This section describes how to authenticate with the credentials for a custom OAuth app. See Creating a Custom OAuth App for more information.
Get an OAuth Access Token
After setting the following properties, you are ready to connect:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- CallbackURL: Set this to the Redirect URL in your app settings.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken. .
When you connect, the provider opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The provider then completes the following OAuth process:
- Extracts the access token from the callback URL and authenticates requests.
- Obtains a new access token when the old one expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Web Authentication with your OAuth App
When connecting via a Web application, you need to register a custom OAuth app with FreshBooks. See Creating a Custom OAuth App for more information. You can then use the provider to get and manage the OAuth token values.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
You can then call stored procedures to complete the OAuth exchange:
- Call the GetOAuthAuthorizationURL stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your app settings. If necessary, set the Permissions parameter to request custom permissions.
The stored procedure returns the URL to the OAuth endpoint.
- Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the Permissions parameter to request custom permissions.
To connect to data, set the OAuthAccessToken connection property to the access token returned by the stored procedure. When the access token expires after ExpiresIn seconds, call GetOAuthAccessToken again to obtain a new access token.
Headless Machines
Using OAuth on a Headless Machine
To create FreshBooks data sources on headless servers or other machines on which the provider cannot open a browser, you need to authenticate from another machine. Authentication is a two-step process.
- Instead of installing the provider on another machine, you can follow the steps below to obtain the OAuthVerifier value. Or, you can install the provider on another machine and transfer the OAuth authentication values, after you authenticate through the usual browser-based flow.
- You can then configure the provider to automatically refresh the access token from the headless machine.
You can follow the headless OAuth authentication flow using the provider's embedded OAuth credentials or using the OAuth credentials for your custom OAuth app.
Using the Credentials for a Custom OAuth App
Create a Custom OAuth App
See Creating a Custom OAuth App for a procedure. You can then follow the procedures below to authenticate and connect to data.
Obtain a Verifier Code
Set the following properties on the headless machine:
- InitiateOAuth: Set this to OFF.
- OAuthClientId: Set this to the App Id in your app settings.
- OAuthClientSecret: Set this to the App Secret in your app settings.
You can then follow the steps below to authenticate from another machine and obtain the OAuthVerifier connection property.
- Call the GetOAuthAuthorizationURL stored procedure with the CallbackURL input parameter set to the exact Redirect URI you specified in your app settings.
- Open the returned URL in a browser. Log in and grant permissions to the provider. You are then redirected to the callback URL, which contains the verifier code.
- Save the value of the verifier code. You will set this in the OAuthVerifier connection property.
On the headless machine, set the following connection properties to obtain the OAuth authentication values:
- OAuthClientId: Set this to the consumer key in your app settings.
- OAuthClientSecret: Set this to the consumer secret in your app settings.
- OAuthVerifier: Set this to the verifier code.
- OAuthSettingsLocation: Set this to persist the encrypted OAuth authentication values to the specified file.
- InitiateOAuth: Set this to REFRESH.
Connect to Data
After the OAuth settings file is generated, set the following properties to connect to data:
- OAuthSettingsLocation: Set this to the file containing the encrypted OAuth authentication values. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- InitiateOAuth: Set this to REFRESH.
Transfer OAuth Settings
Follow the steps below to install the provider on another machine, authenticate, and then transfer the resulting OAuth values.
On a second machine, install the provider and connect with the following properties set:
- OAuthSettingsLocation: Set this to a writable text file.
- InitiateOAuth: Set this to GETANDREFRESH.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- CallbackURL: Set this to the Callback URL in your app settings.
Test the connection to authenticate. The resulting authentication values are written, encrypted, to the path specified by OAuthSettingsLocation. Once you have successfully tested the connection, copy the OAuth settings file to your headless machine. On the headless machine, set the following connection properties to connect to data:
- InitiateOAuth: Set this to REFRESH.
- OAuthSettingsLocation: Set this to the path to your OAuth settings file. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
Creating a Custom OAuth App
Connecting to the Classic API
There are two methods you can use to connect to the FreshBooks Classic API, the authentication token specific to your login or OAuth 1.0. The authentication token method is deprecated and will not be supported by FreshBooks in the future.
Using the Authentication Token to Connect to FreshBooks
To connect to FreshBooks using an authentication token, specify the CompanyName and Token connection properties. The token can be found by logging in to FreshBooks and navigating to My Account > FreshBooks API.
Using OAuth to Connect to FreshBooks
OAuth requires the authenticating user to interact with FreshBooks using the browser. The provider facilitates this in various ways as described in the following sections.
Register Your Application
To obtain the OAuth client credentials:
- Request Developer Access through FreshBooks, if you have not already done so.
- Select My Account > FreshBooks API.
- Select the Use OAuth option and enter the application details. The details are displayed to users when they log in to grant permissions to the application. The OAuth consumer secret is displayed.
Note: It may take some time for FreshBooks to approve your registration.
Authenticate to FreshBooks from a Desktop Application
After setting the following connection properties, you are ready to connect:
- OAuthClientId: Set this to the name of the company you are connecting to. Note that you can also use the CompanyName.
- OAuthClientSecret: Set this to the consumer secret in your app settings.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the access token in the connection string.
When you connect, the provider opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The provider then completes the following OAuth process:
- Retrieves the OAuthAccessToken and OAuthAccessTokenSecret and authenticates requests.
- Refreshes the OAuthAccessToken when it expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Authenticate to FreshBooks from a Web Application
To obtain the access token, set the following connection properties and follow the steps below:
- OAuthClientId: Set this to the name of the company you are connecting to. Note that you can also use the CompanyName property.
- OAuthClientSecret: Set this to the consumer secret in your app settings.
When you connect via a Web application, or if the provider is not authorized to open a browser window, you need to exchange temporary verification values for the access token:
- Call GetOAuthAuthorizationURL. The stored procedure returns the URL to the FreshBooks OAuth endpoint.
- Log in at the OAuth endpoint and authorize the app. You are redirected back to the callback URL.
The callback URL contains the verifier code in a query string parameter. The parameter is named "oauth_verifier". Extract the verifier code from the callback URL.
- Call GetOAuthAccessToken. The stored procedure returns the access token.
To connect to data, set the following connection properties:
- CompanyName
- OAuthClientSecret
- OAuthAccessToken
- OAuthAccessTokenSecret
Refresh the Token
To automatically refresh the access token when it expires, set InitiateOAuth to REFRESH and set OAuthRefreshToken. When the access token expires, call GetOAuthAccessToken to refresh the access token.
Connect to the Alpha API
Use the OAuth 2.0 authentication standard to authenticate to the FreshBooks Alpha APIs.
OAuth requires the authenticating user to interact with FreshBooks using the browser. The provider facilitates this in various ways as described in the following sections.
Register Your Application
To obtain the OAuth client credentials:
- Log into the FreshBooks developers site at https://my.freshbooks.com/#/developer and click Create an App.
- Enter information to be displayed to your users when they are prompted to grant permissions to your app.
- Specify a redirect URI.
Set the redirect URI to https://localhost:33333/, or some other similar https url.
If you are making a Web application, set the Callback URL to a page on your Web app you would like the user to be returned to after they have authorized your application.
Authenticate to FreshBooks from a Desktop Application
After setting the following connection properties, you are ready to connect:
- OAuthClientId: Set this to the name of the company you are connecting to. Note that you can also use the CompanyName.
- OAuthClientSecret: Set this to the consumer secret in your app settings.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the access token in the connection string.
- AccountId: Set this to connect to data. Query the IdentityInfo view to obtain this value.
When you connect, the provider opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The provider then completes the following OAuth process:
- Retrieves the OAuthAccessToken and OAuthAccessTokenSecret and authenticates requests.
- Refreshes the access token when it expires.
- Saves OAuth values to be persisted across connections.
Authenticate to FreshBooks from a Web Application
To obtain the access token, set the following connection properties:
- OAuthClientId: Set this to the name of the company you are connecting to. Note that you can also use the CompanyName property.
- OAuthClientSecret: Set this to the consumer secret in your app settings.
When you connect via a Web application, or if the provider is not authorized to open a browser window, you need to exchange temporary verification values for the access token:
- Call GetOAuthAuthorizationURL. The stored procedure returns the URL to the FreshBooks OAuth endpoint.
- Log in at the OAuth endpoint and authorize the app. You are redirected back to the callback URL.
The callback URL contains the verifier code in a query string parameter. The parameter is named "oauth_verifier". Extract the verifier code from the callback URL.
- Call GetOAuthAccessToken. The stored procedure returns the access token.
To connect to data, set the following connection properties:
- AccountId
- OAuthClientSecret
- OAuthAccessToken
- OAuthAccessTokenSecret
Note: To obtain an AccountId, query the IdentityInfo view to obtain this value.
Refresh the Token
To automatically refresh the access token when it expires, set InitiateOAuth to REFRESH and set OAuthRefreshToken. Alternatively, when the access token expires, call the RefreshOAuthAccessToken stored procedure to refresh the access token.
Customizing the SSL Configuration
By default, the provider attempts to negotiate SSL/TLS by checking the server's certificate against the system's trusted certificate store. To specify another certificate, see the SSLServerCert property for the available formats to do so.
Connecting Through a Firewall or Proxy
HTTP Proxies
To connect through the Windows system proxy, you do not need to set any additional connection properties. To connect to other proxies, set ProxyAutoDetect to false.
In addition, to authenticate to an HTTP proxy, set ProxyAuthScheme, ProxyUser, and ProxyPassword, in addition to ProxyServer and ProxyPort.
Other Proxies
Set the following properties:
- To use a proxy-based firewall, set FirewallType, FirewallServer, and FirewallPort.
- To tunnel the connection, set FirewallType to TUNNEL.
- To authenticate, specify FirewallUser and FirewallPassword.
- To authenticate to a SOCKS proxy, additionally set FirewallType to SOCKS5.
Troubleshooting the Connection
To show provider activity from query execution to network traffic, use Logfile and Verbosity. The examples of common connection errors below show how to use these properties to get more context. Contact the support team for help tracing the source of an error or circumventing a performance issue.
- Authentication errors: Typically, recording a Logfile at Verbosity 4 is necessary to get full details on an authentication error.
- Queries time out: A server that takes too long to respond will exceed the provider's client-side timeout. Often, setting the Timeout property to a higher value will avoid a connection error. Another option is to disable the timeout by setting the property to 0. Setting Verbosity to 2 will show where the time is being spent.
- The certificate presented by the server cannot be validated: This error indicates that the provider cannot validate the server's certificate through the chain of trust. If you are using a self-signed certificate, there is only one certificate in the chain.
To resolve this error, you must verify yourself that the certificate can be trusted and specify to the provider that you trust the certificate. One way you can specify that you trust a certificate is to add the certificate to the trusted system store; another is to set SSLServerCert.