Generate Access Token (AUTH) using Refresh Token for Zoho API using C#

This article will help you to generate access token for zoho api calls using the refresh token for the C# application.

Here is the blog to get the refresh token for the application using the client ID. Please review the link

How To Generate Access Token And Refresh Token For Server Based Application In Zoho Using Postman – The Code Hubs


Add the keys in your web config file

<add key="ZohoClientId" value="1000.XXXXXXXXXXXXXXXXXXXXXXX" />

Add the below class to deserialize the response from zoho

public class ZohoRefreshTokenResponse
      public string access_token { get; set; }
      public string api_domain { get; set; }
      public string token_type { get; set; }
      public string expires_in { get; set; }


Use the below code to call the refresh token API

 string url = "";
 string postData = "client_id=" + ConfigurationManager.AppSettings["ZohoClientId"].ToString() 
                 + "&client_secret=" + ConfigurationManager.AppSettings["ZohoClientSecret"].ToString() 
                 + "&refresh_token=" + ConfigurationManager.AppSettings["ZohoRefreshToken"].ToString() 
                 + "&grant_type=refresh_token";

 byte[] data = Encoding.ASCII.GetBytes(postData);

 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
 var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

 httpWebRequest.ContentType = "application/x-www-form-urlencoded";
 httpWebRequest.Method = "POST";
 Stream requestStream = httpWebRequest.GetRequestStream();
 requestStream.Write(data, 0, data.Length);

 var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
 if (httpResponse.StatusCode == HttpStatusCode.OK || httpResponse.StatusCode == HttpStatusCode.Created)
     using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.Default))
         result = streamReader.ReadToEnd();
 if (!string.IsNullOrEmpty(result))
     var res = Newtonsoft.Json.JsonConvert.DeserializeObject<ZohoRefreshTokenResponse>(result);
     return res.access_token;

Using the above code you can get the new access token. That token we can use to call the other zoho API.




Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories