12 Aug, 2021

Climate Fieldview Authentication with Leaf

Climate Fieldview Authentication with Leaf

In this post, we are going to show you how to easily go through Climate FieldView authorization flow with Leaf, that is, get authorized to access your users’ Climate FieldView data with Leaf. Let’s get to it!

(Clique aqui para a tradução em português)


Did you know the quickest and simplest way to enable provider authentication is through Magic Link or Leaf Link? Leaf provides pre-built widgets (Leaf Link) and shareable URLS (Magic Link) so you can save time on building UI. Talk to us to learn more about pricing.

Summary

  • Step 0: Create a developer account with Climate FieldView, get your application credentials and verify your scopes.
  • Step 1: Get your users’ Climate FieldView tokens with Leaf.
  • Step 2: Add Climate FieldView credentials to Leaf.

Keep in mind that this tutorial is a full walkthrough. Most of the steps presented will be done only once, meaning that subsequent calls will be much easier and faster.

Step 0: Create a developer account with Climate FieldView and get your application credentials.

(If you already have a developer account with Climate FieldView, you can skip this step)

To start the process to setup your Cliamte FieldView developer acount, you can register with this link.

Leaf, data infrastructure for agriculture

After being approved by Climate FieldView, you will receive a clientId, a clientSecret, and an API Key.

Note: Before you proceed, we recommend you email developer@climate.com and verify you have the correct scopes.. "asHarvested:read", "asPlanted:read", "asApplied:read", "fields:read", "resourceOwners:read", "farmOrganizations:read"

Step 1: Get your users’ Climate FieldView tokens with Leaf.

To get your users’ tokens to access their Climate FieldView data, your application must go through Climate FieldView OAuth flow. You'll need to redirect the user to a custom URL that will present the user the choice of authorizing your app to access their data.

To get the URL for this authentication step, send a POST to:

https://api.withleaf.io/ services/usermanagement/ api/users/yourLeafUserId/ climate-field-view-credentials

With the following JSON:

{
"client_id": "Your client ID",
"redirect_uri": "Your company URL"
}

If you prefer, you can also include the pass_through parameter, to inform the desired state after the authentication.

Leaf will create a URL containing the minimum scopes (permissions) necessary to fetch Fields, Farms and Operations. Leaf also supports the upload of Fields, Prescriptions, Soil and GeoTiff data. If you want to access those extra features, add "scope" to the request body including the base scopes like fields:write, rx:write, soil:write and imagery:write. The request body to fetch a URL with the extra scopes should be:

{
"client_id": "Your client ID",
"scope": [
"asHarvested:read",
"asPlanted:read",
"asApplied:read",
"fields:read",
"resourceOwners:read",
"farmOrganizations:read",
"fields:write",
"rx:write",
"soil:write",
"imagery:write"
],
"redirect_uri": "Your app's redirect URL"
}

Note that the scopes for the extra features (fields:write, soil:write and imagery:write) may also require additional permissions from Climate FieldView. Ensure that you are allowed to ask for those scopes before generating a URL with them, and contact Climate FieldView to request permission in case you are not.

Leaf will return a URL that you can send your user to, so they can authorize access to their data and be redirected to your app. Note that Climate FieldView will redirect your user to the "redirect_uri" and also send a code that you will need to make a request to get your users' tokens.

Get the user’s tokens

Now we need to get the user’s tokens. Once users log in to Climate FieldView's website link you created, they will authorize your application to access their data and they will be redirected back to the "redirect_uri" you set, linked with a code. Then, your application will use this code present in the response URL to get the user’s tokens.

Send a POST to:

https://api.climate.com/ api/oauth/token

With the following header:

Authorization: Basic base64(clientId:
clientSecret)

This requires you to encode “clientId:clientSecret” (your cliendId and clientSecret from the first step, separated by “:”) to base64. Most programming languages will have a way to encode text to base64, but if you want to do it manually you can use this website.

Also, include the following body (x-www-form-urlencoded):

"code": "The code received on the last step",
"redirect_uri": "Your app's redirect URL (same as last step)",
"grant_type": "authorization_code"

A JSON containing the tokens will be returned, and you can add them to Leaf as Climate FieldView credentials.

Note: Leaf will manage each and every token, making sure they are always refreshed and valid.

Step 2: Add your Climate FieldView credentials to Leaf

When you integrate with a provider via Leaf, you'll need to create something called a 'Leaf User', which is used to manage provider authentication and organize data for your end users. In most cases, the Leaf User will be the farmer or consultant that has access to the source data from Climate FieldView.

To send your credentials to Leaf, you will need your access token from Leaf, and have already created a Leaf User.

Here is how you get your access token.
Here is how you create a Leaf User.

Now, attach your credentials to the Leaf User you created. Send a POST to:

https://api.withleaf.io/ services/usermanagement/ api/users/yourLeafUserId/ climate-field-view-credentials

With the following header:

Authorization: Bearer

and with the following JSON body:

{
"clientId": "Your client ID",
"clientSecret": "Your client secret",
"apiKey": "Your api key",
"refreshToken": "The refresh token received on the last step"
}

Excellent

Now Leaf will start fetching your farm data from Climate FieldView. You can now query Leaf for your standardized data.

Software developers use Leaf’s API to build and scale a wide range of products including farm optimization tools, lending products, outcome-based financing, land and input marketplaces, agronomic recommendations, traceability applications, equipment maintenance forecasting, and more.

Ready to begin?

Get a Demo and Start Building Today!