ZOHO

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="ZohoRefreshToken" value="1000.XXXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXX" />
<add key="ZohoClientId" value="1000.XXXXXXXXXXXXXXXXXXXXXXX" />
<add key="ZohoClientSecret" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />

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 = "https://accounts.zoho.com/oauth/v2/token";
 
 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);
 requestStream.Close();

 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.

 

 

 

Shailja Jariwala

Back end .NET developer & programmer, Specialized in developing creative ASP.NET MVC applications, websites & portals, having 6+ years of unmatched experience in building innovative, highly customized, robust, feature-packed and modular .NET solutions.

Share
Published by
Shailja Jariwala

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago