public async Task CreateZoomMeeting(string orgName, string appointmentId)
{
var messageList = new List<KeyValuePair<string, string>>();
if (string.IsNullOrEmpty(orgName))
messageList.Add(new KeyValuePair<string, string>("Information", string.Format("Organization is Required")));
if (string.IsNullOrEmpty(appointmentId))
messageList.Add(new KeyValuePair<string, string>("Information", string.Format("Appointment Id is Required. Save Appointment and Proceed")));
if (messageList.Count > 0)
{
TempData["ValidationSummary"] = messageList;
return RedirectToAction("Index", "Home");
}
TempData["orgName"] = orgName;
TempData.Keep("orgName");
TempData["appointmentId"] = appointmentId;
TempData.Keep("appointmentId");
ZoomTokenModel zoomTokenModel = new ZoomTokenModel();
ResponseRoot resposneObj = new ResponseRoot();
string _response = string.Empty;
try
{
service = Connections.ConnectToCRM(service, orgName);
Tokens = Connections.TokenCollection(service, Tokens);
appointmentCollection = Connections.zoomCollection(service, appointmentId, appointmentCollection);
Entity appointmentEntity = appointmentCollection.Entities.Where(x => x.Id.ToString().ToLower() == appointmentId.ToLower().ToString()).FirstOrDefault();
if (appointmentEntity == null)
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Appointment details does not exists!")};
return RedirectToAction("Index", "Home");
}
if (string.IsNullOrEmpty(appointmentEntity.Id.ToString()))
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Appointment details does not exists!")};
return RedirectToAction("Index", "Home");
}
if (Tokens != null && Tokens.Entities.Count > 0)
{
Root rootObj = new Root();
var tokenEntity = Tokens.Entities.FirstOrDefault();
var accessTokenValue = tokenEntity.Attributes.FirstOrDefault(x => x.Key == "accesstokenvalue").Value;
var zen_refreshtoken = tokenEntity.Attributes.FirstOrDefault(x => x.Key == "refreshtoken").Value;
string clientId = ConfigurationManager.AppSettings["clientId"].ToString();
string clientSecret = ConfigurationManager.AppSettings["clientSecret"].ToString();
// Recommended - do refresh access token each time we fire the api
if (!string.IsNullOrEmpty(accessTokenValue.ToString()))
{
try
{
using (var httpClient = new HttpClient())
{
string urlString = "https://zoom.us/oauth/token?grant_type=refresh_token&refresh_token=refresh_token$";
urlString = urlString.Replace("refresh_token$", zen_refreshtoken.ToString());
using (var request = new HttpRequestMessage(new HttpMethod("POST"), urlString))
{
string authorizationString = "Basic " + Base64Encode(clientId + ":" + clientSecret).ToString();
request.Headers.TryAddWithoutValidation("Authorization", authorizationString);
var response = await httpClient.SendAsync(request);
zoomTokenModel = GetResponse<ZoomTokenModel>(response).Result;
}
try
{
if (!tokenEntity.Attributes.Contains("accesstokenvalue"))
{
tokenEntity.Attributes.Add("accesstokenvalue", zoomTokenModel.access_token);
}
else
{
tokenEntity["zen_accesstokenvalue"] = zoomTokenModel.access_token;
}
if (!tokenEntity.Attributes.Contains("refreshtoken"))
{
tokenEntity.Attributes.Add("refreshtoken", zoomTokenModel.refresh_token);
}
else
{
tokenEntity["refreshtoken"] = zoomTokenModel.refresh_token;
}
if (!tokenEntity.Attributes.Contains("apptype"))
{
tokenEntity.Attributes.Add("apptype", new OptionSetValue(282230002));
}
else
{
tokenEntity["apptype"] = new OptionSetValue(282230002);
}
service.Update(tokenEntity);
}
catch(Exception ex)
{
//
}
}
}
catch (Exception ex)
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", ex.Message)};
return RedirectToAction("Index", "Home");
}
}
if (string.IsNullOrEmpty(zoomTokenModel.access_token))
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Unauthorized")};
return RedirectToAction("Index", "Home");
}
if (appointmentEntity != null)
{
if (!string.IsNullOrEmpty(appointmentEntity.Id.ToString()))
{
#start Reference
// we are passing input parameters start_time,end_time and topic to zoom
rootObj.start_time = appointmentEntity.Attributes.FirstOrDefault(x => x.Key == "scheduledstart").Value.ToString();
rootObj.end_time = appointmentEntity.Attributes.FirstOrDefault(x => x.Key == "scheduledend").Value.ToString();
rootObj.topic = appointmentEntity.Attributes.FirstOrDefault(x => x.Key == "subject").Value.ToString();
DateTime startTime = DateTime.Parse(resposneObj.start_time);
DateTime endTime = DateTime.Parse(rootObj.end_time);
startTime.ToString("HH:mm");
endTime.ToString("HH:mm");
TimeSpan difference = endTime - startTime;
int hours = difference.Hours;
int minutes = difference.Minutes;
BigInteger _minutes = hours * 60;
BigInteger _totalDuration = _minutes + minutes;
rootObj.duration = Convert.ToString(_totalDuration);
var jsonObj = new JavaScriptSerializer().Serialize(rootObj);
var client = new RestClient("https://api.zoom.us/v2/users/me/meetings");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "Bearer" + zoomTokenModel.access_token);
request.AddParameter("application/json", jsonObj, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.StatusCode.ToString() == "Unauthorized")
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Unauthorized")};
return RedirectToAction("Index", "Home");
}
resposneObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseRoot>(response.Content);
try
{
string title = "Zoom Meeting Details";
var desc = "<br>" + "<br><b>" + title + "</b>" + "<br>" + "<br>" + "Topic:" + " " + resposneObj.topic + "<br>" + "Time:" + " " + resposneObj.start_time + "<br>" + "<br>" + "Join Zoom Meeting" + "<br>" + "<a href>" + resposneObj.join_url + "</a>" + "<br>" + "<br>" + "Meeting ID:" + " " + resposneObj.id + "<br>" + "Passcode:" + " " + resposneObj.password;
end Reference
if (appointmentEntity.Attributes.Contains("description"))
{
appointmentEntity["description"] = appointmentEntity["description"] + desc;
}
else
{
appointmentEntity.Attributes.Add("description", desc);
}
service.Update(appointmentEntity);
}
catch(Exception ex)
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Failed to Update Zoom Meeting - " + ex.Message)};
return RedirectToAction("Index", "Home");
}
}
}
}
else
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Zoom Single-Singon Authorization is Required!")};
return RedirectToAction("Index", "Home");
}
}
catch (Exception ex)
{
TempData["ValidationSummary"] = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Error", "Unauthorized - " + ex.Message)};
return RedirectToAction("Index", "Home");
}
return View("Success");
}
note:- please refer the above code from #Start Reference to #endReference region code for Request body of zoom.