The OAuth 2 protocol is used to authenticate and authorize every API request. OAuth makes it possible for you to act on behalf of Zengine users without those users needing to share their credentials (username/password). This is made possible via a flow where the end user is redirected from your application to Zengine and grants your application permission to an access token for a finite period of time. Additionally,
You do not necessarily need to act on behalf of a user; you can be the end user. For example, you may be creating a script to run every night and extract data from your workspace. In this scenario, you must authenticate via the OAuth flow, retrieve a token for your account, and hard code that token into your script.
You can retrieve any API key by creating a user, and then using the access token returned to you to create an API client. Take note of the key that is returned to you. It cannot be retrieved again.
Your application must specify a redirect URI. This is the URI the end user will return to upon authenticating with Zengine. This URL must be set up to look for an OAuth response and process it accordingly.
There are two types of requests you may want to make, both requiring different sets of credentials:
Most resources in the Zengine API are private.
To access public resources, you will need to provide an API key in the query string of your request:
?client_id={your API key}
To access private resources, you will need to provide an access token for the user you are acting on behalf of (even if that user is yourself).
?access_token={user's access token}
A 3-legged flow is require in a scenario where you want to access private data on behalf of an end user. You may want to make your API calls from a server-side script (Ruby, Python, PHP, etc) or from a web client (HTML + JS). Because server-side scripts are implicitly more secure than clients, these two scenarios require different flows.
The following describes a typical server-side flow:
The access token you have now lasts for 24 hours. You can use it to make API requests which require authorization.
Once your token expires, you may use your refresh token (provided in Step 5) to retrieve a new access token:
GET https://auth.zenginehq.com/oauth2/v1/grant?client_id={your API key}&client_secret={your API secret}&redirect_uri={the redirect URI associated with your application}&grant_type=refresh_token&refresh_token={the refresh token you previously received from Zengine}
A sample server-side script, written in PHP, is provided below:
The following describes a typical client-side flow:
The access token you have now lasts for 1 hour. You can use it to make API requests which require authorization.
Once your token expires, you must send the user back to Zengine to retrieve a new token.
A sample client-side script is provided below:
An endpoint for Authorizations is made available in the API. This makes it possible for you to handle a user's third party application authorizations on behalf of that user. This is primarily provided as a way to allow your own application to remove itself from the user's authorizations list. This comes in handy if the user asks your application to disconnect from Zengine.