protected void Page_Load(object sender, EventArgs e) { log.Info("Started Page Load"); lblError.Text = ""; try { ZohoBookAuthCodeUrl = ConfigurationManager.AppSettings["ZohoBook-AuthCode-Url"]; ZohoBookAccessTokenUrl = ConfigurationManager.AppSettings["ZohoBook-AccessToken-Url"]; ZohoBookRefreshTokenUrl = ConfigurationManager.AppSettings["ZohoBook-RefreshToken-Url"]; ZohoBookClientCode = ConfigurationManager.AppSettings["ZohoBook-ClientCode"]; ZohoBookClientSecret = ConfigurationManager.AppSettings["ZohoBook-ClientSecret"]; ZohoBookItemsScope = ConfigurationManager.AppSettings["ZohoBook-ItemsScope"]; ZohoBookRedirectUrl = ConfigurationManager.AppSettings["ZohoBook-RedirectUrl"]; ZohoBookOrgId = ConfigurationManager.AppSettings["ZohoBook-OrgId"]; if (!IsPostBack) { //Check if code is available in querystring if (Request.QueryString["code"] != null) { log.Info("Access Token Flow. Auth Code is - " + Request.QueryString["code"].ToString()); //Access Token flow string code = Request.QueryString["code"].ToString(); string authUrl = string.Concat(ZohoBookAccessTokenUrl, "?grant_type=authorization_code&client_id={0}&client_secret={1}&redirect_uri={2}&code={3}"); ApiModel model = new ApiModel(); model.RelativeUrl = string.Format(authUrl, ZohoBookClientCode, ZohoBookClientSecret, ZohoBookRedirectUrl, code); model.Method = HttpMethod.Post; log.Info("Auth Url - " + model.RelativeUrl); var response = ApiHelper.ExecuteAPI(model); if (response != null && response.IsSuccessStatusCode) { var jsonResponse = response.Content.ReadAsStringAsync().Result; JObject jobject = JObject.Parse(jsonResponse); Session["Zoho-AccessToken"] = jobject.ContainsKey("access_token") ? jobject["access_token"].ToString() : ""; Session["Zoho-RefreshToken"] = jobject.ContainsKey("refresh_token") ? jobject["refresh_token"].ToString() : ""; UpdateConfig("ZohoBook-RefreshToken", Session["Zoho-RefreshToken"].ToString()); log.Info("Response From Access Token API - " + jsonResponse); btnAuthenticate.Enabled = false; Response.Redirect("ZohoImport.aspx?auth=success"); } else { var jsonResponse = response.Content.ReadAsStringAsync().Result; log.Info("Error From Access Token API - " + jsonResponse); } } else if (Request.QueryString["auth"] != null && Request.QueryString["auth"].ToString() == "success") { lblError.Text = "Successfully Authenticated"; btnAuthenticate.Enabled = false; } else if (Session["Zoho-AccessToken"] != null) { btnAuthenticate.Enabled = false; } else { //Auth Code flow //It should be triggered from Authenticate button } } } catch (Exception ex) { log.Error("Exception - " + ex.ToString()); } log.Info("Completed Page Load"); }
private bool ImportItems() { log.Info("Import Started"); try { if (Session["Zoho-AccessToken"] != null) { int pageSize = 200; int pageIndex = 1; bool hasMorePages = true; List <ItemsModel> gridItems = new List <ItemsModel>(); while (hasMorePages) { //Access Token found string itemsUrl = string.Format(ConfigurationManager.AppSettings["ZohoBook-Items"] + "organization_id={0}", ZohoBookOrgId); itemsUrl = string.Format(itemsUrl + "&page={0}&per_page={1}", pageIndex, pageSize); log.Info("Items Url - " + itemsUrl); ApiModel model = new ApiModel(); model.RelativeUrl = itemsUrl; model.Method = HttpMethod.Get; model.Headers = new Dictionary <string, string>(); model.Headers.Add("Authorization", "Bearer " + Session["Zoho-AccessToken"]); var response = ApiHelper.ExecuteAPI(model); if (response != null && response.IsSuccessStatusCode) { var jsonResponse = response.Content.ReadAsStringAsync().Result; JObject jobject = JObject.Parse(jsonResponse); log.Info("Items Data - " + jsonResponse); JArray itemArray = (JArray)jobject["items"]; if (!(bool)jobject["page_context"]["has_more_page"]) { hasMorePages = false; } List <ItemsModel> items = new List <ItemsModel>(); SqlHelper sqlHelper = new SqlHelper(); foreach (JObject item in itemArray) { ItemsModel itemModel = new ItemsModel(); itemModel.ItemId = item["item_id"].ToString(); itemModel.Name = item["name"].ToString(); itemModel.Unit = item["unit"].ToString(); itemModel.Description = item["description"].ToString(); itemModel.ItemType = item["item_type"].ToString(); itemModel.AvailableStock = item["available_stock"].ToString(); itemModel.SKU = item["sku"].ToString(); itemModel.Category = item["cf_master_category"].ToString(); itemModel.SubCategory = item["cf_sub_category"].ToString(); itemModel.SellingPrice = item["rate"].ToString(); itemModel.StockOnHand = item["stock_on_hand"].ToString(); itemModel.Status = item["status"].ToString(); itemModel.Brand = item["cf_brand"].ToString(); items.Add(itemModel); sqlHelper.ImportData(itemModel); } gridItems.AddRange(items); sqlHelper.CloseConnection(); lblError.Text = "Import Completed"; log.Info("Import Completed"); } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { lblError.Text = "Token Expired. Please Re-Authenticate"; return(false); } else { var jsonResponse = response.Content.ReadAsStringAsync().Result; log.Info("Error from Items API - " + jsonResponse); lblError.Text = jsonResponse; return(false); } pageIndex = pageIndex + 1; } grdItems.DataSource = gridItems; grdItems.DataBind(); } else { lblError.Text = "Token Expired. Please Autehnticate"; return(false); } return(true); } catch (Exception ex) { log.Error("Exception - " + ex.ToString()); lblError.Text = ex.Message; return(false); } }