Integration Guide
The OAuth 2.0 flow a Marketplace partner implements to install an integration and call the API.
The AgriWebb Marketplace uses the OAuth 2.0 Authorization Code Grant to issue access tokens to partners. A sample app that implements the flow is on GitHub. The base URLs and endpoints for production and staging are on Environments.
OAuth 2.0 flow
When a user selects to install an integration from the AgriWebb Marketplace, we will initiate the authorisation flow with the nominated partner.
Step 1 - Install
Partner endpoint implementation required
Please see the Partner API Reference - Install Endpoint for technical specifications.
Once a user has selected to install an integration, AgriWebb will redirect the user's browser to the partner's Install URL. The partner's install endpoint begins the OAuth 2.0 Authorization Code Grant flow, where the partner (the OAuth 2.0 client) creates an authorisation request to the AgriWebb auth server (the OAuth 2.0 authorisation server). The install endpoint redirects the user's browser to the Authorisation Request route with the required parameters.
Step 2 - Authorisation Request
Please see the API Reference - Authorisation Request for technical specifications.
To begin the OAuth 2.0 Authorization Code Grant flow, the partner must redirect the user to the Authorisation Request endpoint. Our implementation requires that if you receive a parameter called organization, you must pass it through to the AgriWebb auth server; this prevents the user from having to re-input their organisation.
For example, with every value URL-encoded:
https://auth.agriwebb.com/oauth2/authorize?response_type=code&client_id={client ID}&redirect_uri={redirect URL}&scope=read:farms%20read:animals&state={state}&organization={organisation ID}Step 3 - Authorisation response
Partner endpoint implementation required
Please see the Partner API Reference - Callback Endpoint for technical specifications.
The browser session will be redirected to the Redirect URL (also referred to as the Callback Endpoint) when the user has accepted the partner's requested permissions (or denied the request). This endpoint is where most of the OAuth 2.0 logic will lie in the partner's implementation. If you require users to login, sign up, or accept any terms and conditions for your application here is where we recommend you implement this.
When the user approves the request, the browser arrives at:
{redirect URL}?code={code}&state={state}Step 4 - Token Exchange
Please see the API Reference - Token Exchange for technical specifications.
The Token Exchange endpoint can now be called using the code received in the authorisation response; this token exchange will provide the partner with a set of credentials, including an access token and a refresh token.
curl https://auth.agriwebb.com/oauth2/token \
--header 'Authorization: Basic {base64 of Client ID:Client Secret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'code={code}' \
--data-urlencode 'redirect_uri={redirect URL}'The response:
{
"access_token": "{access token}",
"token_type": "Bearer",
"expires_in": 43200,
"refresh_token": "{refresh token}"
}Step 5 - Set integration status
Please see the API Reference - Marketplace Callback for technical specifications.
Using the credentials received from the Token Exchange, the partner must call the Marketplace Callback with an integration-status set to either functional or non-functional. The user must then be redirected back to the AgriWebb Marketplace via the redirect URL provided in the response body.
At any point, you can call the Marketplace Callback with the integration-error type and provide errorUserMessage and errorDeveloperMessage. The errorUserMessage must contain an error message intended for end users. The errorDeveloperMessage must include detailed error information for AgriWebb developers and support staff.
Step 6 - Using the API
Please see the API Reference - GraphQL for technical specifications.
The GraphQL API and the Reports API can now be called with the access token received from the Token Exchange. See Authentication for how to send it.
Step 7 - Refreshing an expired access token
Please see the API Reference - Token Exchange for technical specifications.
The access token expires after expires_in seconds. To get a new set of credentials, call the Token Exchange with the refresh token from the latest token exchange.
curl https://auth.agriwebb.com/oauth2/token \
--header 'Authorization: Basic {base64 of Client ID:Client Secret}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'refresh_token={refresh token}'The response has the same fields as the code exchange. Store the new refresh token: the one you sent is revoked.