Print

Zoho Deluge - Get Refresh/Access Token API v2

What?
A sorta quick article to note how I can generate refresh tokens and access tokens using Zoho Deluge code (so within Zoho Creator, CRM or Books) without XML calls.

Why?
I find myself using this more and more now that API v1 is on the way out and Zoho Deluge by itself is limited somewhat with regards to functionality and record editing when compared to API v2.

How?
Note that this is using Zoho Deluge and not another server-side script such as PHP to send the requests via API.

Following standard OAuth 2.0 procedures, we will get a Code to generate a Refresh token, once we have a Refresh token we will generate an Access token.

Pre-amble
Quit trying to generate an authtoken v1 (CRM Deprecation Sunset Date: 31st December 2019).
  1. Go to the Zoho Developer Console
  2. Create a Zoho Client ID
  3. Either use self-client for a code or use the below to generate one.

Code
Here you will need your own client ID and client secret generated from the developer console in the following snippet:
copyraw
v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234";
v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455";
v_RedirectUri = "https://www.zoho.com/books";  // can be any endpoint that does not redirect or reformat the resulting URL
//  
// get Grant Token
v_EndPoint = "https://accounts.zoho.com/oauth/v2/auth";
v_Scope = "ZohoBooks.contacts.ALL,ZohoBooks.invoices.ALL,ZohoBooks.purchaseorders.ALL";
v_State = "testing";
v_ResponseType = "code";
v_Access = "offline";
v_Prompt = "consent";
l_Params = List();
l_Params.add("scope=" + v_Scope);
l_Params.add("client_id=" + v_ClientID);
l_Params.add("state=" + v_State);
l_Params.add("response_type=" + v_ResponseType);
l_Params.add("redirect_uri=" + v_RedirectUri);
l_Params.add("access_type=" + v_Access);
l_Params.add("prompt=" + v_Prompt);
v_Url = v_EndPoint + "?" + l_Params.toString("&");
info v_Url;
openUrl( v_Url , "new window");
  1.  v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234"
  2.  v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455"
  3.  v_RedirectUri = "https://www.zoho.com/books";  // can be any endpoint that does not redirect or reformat the resulting URL 
  4.  // 
  5.  // get Grant Token 
  6.  v_EndPoint = "https://accounts.zoho.com/oauth/v2/auth"
  7.  v_Scope = "ZohoBooks.contacts.ALL,ZohoBooks.invoices.ALL,ZohoBooks.purchaseorders.ALL"
  8.  v_State = "testing"
  9.  v_ResponseType = "code"
  10.  v_Access = "offline"
  11.  v_Prompt = "consent"
  12.  l_Params = List()
  13.  l_Params.add("scope=" + v_Scope)
  14.  l_Params.add("client_id=" + v_ClientID)
  15.  l_Params.add("state=" + v_State)
  16.  l_Params.add("response_type=" + v_ResponseType)
  17.  l_Params.add("redirect_uri=" + v_RedirectUri)
  18.  l_Params.add("access_type=" + v_Access)
  19.  l_Params.add("prompt=" + v_Prompt)
  20.  v_Url = v_EndPoint + "?" + l_Params.toString("&")
  21.  info v_Url; 
  22.  openUrl( v_Url , "new window")
This will open a page for you with a Zoho Consent form where you confirm whether your app is allowed to access the data within the specified scope(s). If you run this as a standalone, then this will simply tell you what the link is to go to. Agree and note the code parameter passed in the URL (ie. "&code=..."). Also note that any generated code immediately expires any previously generated code.

Refresh Token
With the code you have received via the URL, enter this in the code below to generate a refresh token:
copyraw
v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234";
v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455";
v_RedirectUri = "https://www.zoho.com/books";
//  
// enter the code received in the previous step
v_Code = "1000.00001111222233334444555566667777888899990000aaaabbbbccccddddeeeef";
v_EndPoint = "https://accounts.zoho.com/oauth/v2/token";
v_GrantType = "authorization_code";
m_Payload = Map();
m_Payload.put("code",v_Code);
m_Payload.put("client_id",v_ClientID);
m_Payload.put("client_secret",v_ClientSecret);
m_Payload.put("redirect_uri",v_RedirectUri);
m_Payload.put("grant_type",v_GrantType);
r_AuthToken = invokeurl
[
	url :v_EndPoint
	type :POST
	parameters:m_Payload
];
info r_AuthToken;
  1.  v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234"
  2.  v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455"
  3.  v_RedirectUri = "https://www.zoho.com/books"
  4.  // 
  5.  // enter the code received in the previous step 
  6.  v_Code = "1000.00001111222233334444555566667777888899990000aaaabbbbccccddddeeeef"
  7.  v_EndPoint = "https://accounts.zoho.com/oauth/v2/token"
  8.  v_GrantType = "authorization_code"
  9.  m_Payload = Map()
  10.  m_Payload.put("code",v_Code)
  11.  m_Payload.put("client_id",v_ClientID)
  12.  m_Payload.put("client_secret",v_ClientSecret)
  13.  m_Payload.put("redirect_uri",v_RedirectUri)
  14.  m_Payload.put("grant_type",v_GrantType)
  15.  r_AuthToken = invokeUrl 
  16.  [ 
  17.      url :v_EndPoint 
  18.      type :POST 
  19.      parameters:m_Payload 
  20.  ]
  21.  info r_AuthToken; 
Once you have a refresh token, it is not necessary to generate another refresh token as it does not expire. You would only generate a new one if you were changing scope. There is a limit of 20 refresh tokens. Any refresh tokens generated after will overwrite the oldest one.

Access Token
This will expire after 1 hour so either you find a way to store the access token for reuse or generate a new one in every request.
copyraw
v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234";
v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455";
v_RedirectUri = "https://www.zoho.com/books";
//  
// enter the refresh token received in the previous step
v_RefreshToken = "1000.aaaabbbbccccddddeeeeffff11112222.33334444555566667777888899990000";
v_EndPoint = "https://accounts.zoho.com/oauth/v2/token";
v_GrantType = "refresh_token";
m_Payload = Map();
m_Payload.put("refresh_token",v_RefreshToken);
m_Payload.put("client_id",v_ClientID);
m_Payload.put("client_secret",v_ClientSecret);
m_Payload.put("redirect_uri",v_RedirectUri);
m_Payload.put("grant_type",v_GrantType);
r_AuthToken = invokeurl
[
	url :v_EndPoint
	type :POST
	parameters:m_Payload
];
v_AccessToken = r_AuthToken.get("access_token");
  1.  v_ClientID = "1000.ABCDEFGHIJKLMNOPQRSTUVWXYZ1234"
  2.  v_ClientSecret = "aaaabbbbccccddddeeeeffff111122223333444455"
  3.  v_RedirectUri = "https://www.zoho.com/books"
  4.  // 
  5.  // enter the refresh token received in the previous step 
  6.  v_RefreshToken = "1000.aaaabbbbccccddddeeeeffff11112222.33334444555566667777888899990000"
  7.  v_EndPoint = "https://accounts.zoho.com/oauth/v2/token"
  8.  v_GrantType = "refresh_token"
  9.  m_Payload = Map()
  10.  m_Payload.put("refresh_token",v_RefreshToken)
  11.  m_Payload.put("client_id",v_ClientID)
  12.  m_Payload.put("client_secret",v_ClientSecret)
  13.  m_Payload.put("redirect_uri",v_RedirectUri)
  14.  m_Payload.put("grant_type",v_GrantType)
  15.  r_AuthToken = invokeUrl 
  16.  [ 
  17.      url :v_EndPoint 
  18.      type :POST 
  19.      parameters:m_Payload 
  20.  ]
  21.  v_AccessToken = r_AuthToken.get("access_token")

Now you have an access token you can build your header and send requests:
copyraw
m_Header = Map();
m_Header.put("Authorization","Zoho-oauthtoken " + v_AccessToken);
m_Header.put("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");  // optional
m_Data= Map();
m_Data.put("data",m_RecordData.toJSONList());  // NOTE: convert this to a JSON Array
r_RecordUpdate = invokeUrl
[
	url :v_EndPoint
	type :PUT
	headers: m_Header
	parameters:m_Data.toString() 
];
// NOTE: convert parameters variable to a string
  1.  m_Header = Map()
  2.  m_Header.put("Authorization","Zoho-oauthtoken " + v_AccessToken)
  3.  m_Header.put("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");  // optional 
  4.  m_Data= Map()
  5.  m_Data.put("data",m_RecordData.toJSONList());  // NOTE: convert this to a JSON Array 
  6.  r_RecordUpdate = invokeUrl 
  7.  [ 
  8.      url :v_EndPoint 
  9.      type :PUT 
  10.      headers: m_Header 
  11.      parameters:m_Data.toString() 
  12.  ]
  13.  // NOTE: convert parameters variable to a string 

Common Error(s):
Scope(s) to include ALL modules:
Zoho Books API v3 Limits 2019:
Zoho CRM API v2 Limits 2019:
Source(s):
Category: Zoho :: Article: 687