Generating Access and Refresh Tokens

Generating Access and Refresh Tokens

Generating Access and Refresh Tokens

In this step, your server makes a secure back-end request to Zoho's authorization server. Unlike Step 2, this is a POST request that happens entirely behind the scenes—no user interaction is required.

At Kalki LLP, we emphasize that this is a one-time exchange. The tokens received here should be stored securely in your database for all future API calls.


1. The Token Request (POST)

To exchange your Grant Token (code), send a POST request to the following endpoint based on your data center:

Required Payload Parameters

The request must be sent with the Content-Type: application/x-www-form-urlencoded.

ParameterValueDescription
code{grant_token}The 1000.xxxx code received in Step 2.
client_id{your_client_id}Obtained during Step 1 (Registration).
client_secret{your_client_secret}Obtained during Step 1.
redirect_uri{your_redirect_uri}Must match the URI used in Steps 1 & 2.
grant_typeauthorization_codeHardcoded value for this exchange.

2. Understanding the JSON Response

A successful request will return a JSON object containing the keys to your Zoho Books data:

JSON
{
"access_token": "1000.xxxx.yyyy",
"refresh_token": "1000.zzzz.aaaa",
"api_domain": "https://www.zohoapis.in",
"token_type": "Bearer",
"expires_in": 3600
}

Key Components:

  • access_token: Your temporary key to call the API. It is valid for 1 hour (3600 seconds).

  • refresh_token: Your permanent key. Use this to get a new access token once the current one expires. Note: This is only returned if you used access_type=offline in Step 2.

  • api_domain: The base URL you must use for all subsequent Books API calls (e.g., zohoapis.in).


3. Error Logs & Troubleshooting Step 3

This step is where most developers face "Invalid Code" errors. Use this table to debug:

Error LogProbable CauseFix for Kalki LLP Developers
invalid_codeCode expired or used twice.Grant tokens expire in 1 minute. Ensure you exchange it immediately.
invalid_clientID/Secret mismatch.Verify you are using the correct Secret for the specific Data Center (.in vs .com).
invalid_redirect_uriURI mismatch.Ensure the redirect_uri is identical to what was registered in the console.
server_errorGET request used.This endpoint only accepts POST requests.

4. Best Practices for Token Storage

For kalkillp.com clients, we recommend the following security standards:

  1. Encrypt the Refresh Token: Store it in an encrypted database field. If compromised, it gives permanent access to your books.

  2. Handle the 20-Token Limit: Zoho allows only 20 active refresh tokens per user per app. If you generate a 21st, the 1st one becomes invalid.

  3. Token Scoping: Never request more permissions than needed. If you only need to read invoices, use ZohoBooks.invoices.READ instead of fullaccess.


Automate Your Token Management

Tired of managing manual token refreshes and handling "Invalid Code" errors? Kalki LLP provides custom middleware and Deluge-based automation to handle the entire OAuth lifecycle for you.

    • Related Articles

    • Generating the Grant Token

      Generating the Grant Token (The Authorization Request) Once your application is registered, the next step is to obtain a Grant Token (also known as an Authorization Code). This is a short-lived token (valid for only 1 minute) that you will later ...
    • Refreshing Your Access Token

      Refreshing Your Access Token (Automating the Handshake) An Access Token is like a temporary pass—it expires after one hour. To maintain a continuous connection between your software and Zoho Books without asking the user to log in again, you must use ...
    • Guide to Zoho Books OAuth 2.0

      The Ultimate Guide to Zoho Books OAuth 2.0: Setup, Scopes, and Troubleshooting Authentication is the foundation of any secure integration. The Zoho Books API v3 uses the OAuth 2.0 protocol to ensure that your financial data is accessed securely ...
    • Zoho Books API v3: Enabling Client Portal Access

      Zoho Books API v3: Enabling Client Portal Access The Enable Portal Access API allows you to programmatically invite your customers to the Zoho Books Client Portal. This action sends an automated invitation email to the primary contact person ...
    • Zoho Books API v3: Retrieving Unused Retainer Payments

      Zoho Books API v3: Retrieving Unused Retainer Payments The Get Unused Retainer Payments API returns a list of all retainer payments associated with a specific contact that have a remaining balance. This is essential for workflows where you want to ...