How to register an API that uses OAuth2
This guide shows you how to let a workflow call an API whose access tokens expire, such as Zoho CRM. You need a client ID and a client secret from the provider. For the refresh token grant, you also need a refresh token.
Picked keeps the credential, gets an access token from the provider, and sends the token with each request. Picked gets a new token before the old token expires.
1. Make a refresh token in Zoho
Do this step for Zoho CRM only. For another provider, get the credential from the provider's documentation.
- Open the Zoho API console for the region of your Zoho account.
- Add a client of the type
Self Client. - Make a grant code with the scopes that the workflow needs, such as
ZohoCRM.modules.leads.CREATE. - Send a
POSTrequest to the token URL of your region, such ashttps://accounts.zoho.com.au/oauth/v2/token. Put the parameters in the form body, not in the URL. The parameters aregrant_type=authorization_code,client_id,client_secret, andcodewith the grant code. The grant code expires soon, so do this step immediately. - Keep the
refresh_tokenfrom the answer.
2. Register the external API
- Open Integrations.
- Choose Configure external APIs.
- Choose New external API.
- For Zoho CRM, set Preset to the region of your Zoho account. The preset sets the Display name, the Origin, the Credential and the settings of the OAuth2 credential. Then go to step 10. If Preset does not show your region, do steps 5 to 9.
- Enter a Display name.
- Enter the Origin, such as
https://www.zohoapis.com.au. - Set Credential to OAuth2 credential.
- Enter the Token URL from the provider's documentation. Do not put a secret in the token URL.
- Set the Grant, the Client authentication and the Header scheme from the provider's documentation.
- Enter the Client ID.
- Enter the Client secret.
- For the refresh token grant, enter the Refresh token.
- Choose Register external API.
The client secret and the refresh token are write-only. After you save them, Picked shows only that they are stored.
3. Add a credential to an external API that you registered
- Open the external API.
- Choose Use an OAuth2 credential.
- For Zoho CRM, set Preset to the region of your Zoho account. For another provider, do steps 8 and 9 of section 2.
- Do steps 10 to 12 of section 2.
- Choose Save credential.
The credential replaces the authorization header of the external API.
4. Refresh only for an expired token
Some APIs answer 401 for an error that a new token does not correct. To get a new token only for an expired token:
- Open the external API.
- Choose Edit OAuth2 credential.
- In 401 rule: response body path, enter the path of the error code in the 401 answer,
such as
code. Put a dot between the parts of the path, for exampledata.0.code. - In 401 rule: values, enter the error codes that show an expired token, such as
INVALID_TOKEN. - Choose Save credential.
The Zoho CRM preset sets this rule for you.
5. Replace a refresh token that the provider rejected
When the provider rejects the credential, the external API page shows the reason in Last
refresh failure. Each call fails with credential_rejected until you correct the
credential.
- Make a new refresh token.
- Open the external API.
- Choose Edit OAuth2 credential.
- Enter the new Refresh token.
- Choose Save credential.
Leave the Client secret empty to keep the stored client secret. If you change the Token URL, the Client ID or the Client authentication, enter the client secret and the refresh token again.
Related
- External APIs — every setting of an OAuth2 credential, and when Picked gets a new token.
- How to call an external API from a workflow — the node that sends the request.
