protected override void OnActionExecuting(ActionExecutingContext filterContext) { IsAuthenticated = false; ShopName = string.Empty; base.OnActionExecuting(filterContext); RavenSession = MvcApplication.Store.OpenSession(); var authState = ShopifyAuthorize.GetAuthorizationState(HttpContext); // ONLY DEV!!!!!!!!! if (authState == null) { authState = new ShopifyAuthorizationState { AccessToken = "363fa17b9d1a23b2ebaa53f1ff374c01", ShopName = "jesse-9" }; ShopifyAuthorize.SetAuthorization(HttpContext, authState); } if (authState != null) { ShopifyApi = new ShopifyAPIClient(authState); IsAuthenticated = true; ShopName = authState.ShopName; } ViewBag.IsAuthenticated = IsAuthenticated; ViewBag.ShopName = ShopName; }
protected void Page_Load(object sender, EventArgs e) { ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState; ShopifyAPIClient client = new ShopifyAPIClient(state); APIOutput.Text = (string)client.Get("/admin/products.json"); }
public ActionResult Buy(Guid id, string pid) { ShopifyAuthorizationState authState = Truffle.Shopify.ShopifyAuthorize.GetAuthorizationState(this.HttpContext); var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); WishLuUser user = Graph.GetUserById(id); dynamic prod = api.Get("/admin/products/" + pid + ".json"); dynamic order = new { order = new { line_items = new dynamic[1] { new { @variant_id = prod.product.variants[0].id, @quantity = 1 } }, customer = new { first_name = "Tyler", last_name = "Harden", email = "*****@*****.**" }, billing_address = new { first_name = "Tyler", last_name = "Harden", address1 = "1497 Butterfield Circle", phone = "330-766-0277", city = "Niles", province = "Ohio", country = "United States", zip = "44446" }, shipping_address = new { first_name = user.FirstName, last_name = user.LastName, address1 = user.ShipAddress1, phone = "N/A", city = user.ShipCity, province = user.ShipStateOrProvince, country = "United States", zip = user.ShipZipOrPostalCode }, email = "*****@*****.**" } }; object res = null; try { res = api.Post("/admin/orders.json", order); } catch (WebException ex) { StreamReader sr = new StreamReader(ex.Response.GetResponseStream()); Logger.Log(sr.ReadToEnd()); } return(View("Buy", res)); }
public void ShouldCreateProductUri() { var authState = new ShopifyAuthorizationState() { AccessToken = "beep boop", ShopName = "chucks-chili-dogs" }; var sapi = new ShopifyAPIContext(authState); Assert.AreEqual("/admin/products/67", sapi.ProductPath("67")); }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); ShopifyAuthorizationState authState = Session["shopify_auth_state"] as ShopifyAuthorizationState; if (authState != null) { _shopify = new ShopifyClient(authState); } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); ShopifyAuthorizationState authState = ShopifyAuthorize.GetAuthorizationState(this.HttpContext); if (authState != null) { _shopify = new ShopifyAPIClient(authState, new JsonDataTranslator()); } }
/// <summary> /// Get API Client object with connection to a Shopify store. /// </summary> /// <returns></returns> public ShopifyAPIClient GetAPIClient() { ShopifyAuthorizationState authState = new ShopifyAuthorizationState(); authState.ShopName = this.account; authState.AccessToken = this._token; ShopifyAPIClient api = new ShopifyAPIClient(authState); return(api); }
protected void Page_Load(object sender, EventArgs e) { ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState; ShopifyAPIClient client = new ShopifyAPIClient(state); string shopData = (string)client.Get("/admin/products.json"); JavascriptSerializer serializer = new JavascriptSerializer(); // Here Product is a object class which contains all of the attribute that JSON has. List <Product> lstProduct = serializer(shopData); GridView1.DataSource = lstProduct; GridView1.DataBind(); }
public object DeleteEntity(string entity, string token, string shopName, string id) { var authState = new ShopifyAuthorizationState { AccessToken = token, ShopName = shopName }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); var res = api.Delete($"/admin/{entity}/{id}.json"); return(res); }
public object CreateEntity(string entity, string token, string shopName, object body) { var authState = new ShopifyAuthorizationState { AccessToken = token, ShopName = shopName }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); var res = api.Post($"/admin/{entity}.json", body); return(res); }
public dynamic GetEntities(string entity, string token, string shopName, string[] fields, int top, string collectionId) { var authState = new ShopifyAuthorizationState { AccessToken = token, ShopName = shopName }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); var fieldQuery = ""; var summary = new JArray(); if (fields.Length != 0) { var commaSeparatedFields = string.Join(",", fields); fieldQuery = "fields=" + commaSeparatedFields + "&"; } if (collectionId != null) { fieldQuery = fieldQuery + "collection_id=" + collectionId + "&"; } if (top == 0 || top > 250) { dynamic count = api.Get($"/admin/{entity}/count.json"); var json = count; double realCount = int.Parse(json.GetValue("count").ToString()); realCount = realCount / 250; var loops = (int)Math.Ceiling(realCount); if (entity == "webhooks") { loops = 1; } for (var i = 1; i <= loops; i++) { CollectEntities(api, summary, entity, 250, i, fieldQuery); } } else { CollectEntities(api, summary, entity, top, 1, fieldQuery); } return(summary); }
// // GET: /Shopify/finalize public ActionResult Finalize() { string appGuid = Request.Params["AppGuid"]; string shopName = Request.Params["ShopName"]; string apiKey = Request.Params["ApiKey"]; string sharedSecret = Request.Params["SharedSecret"]; var authorizer = new ShopifyAPIAuthorizer(shopName, apiKey, sharedSecret); // get the following variables from the Query String of the request string error = Request.QueryString["error"]; string code = Request.Params["code"]; string shop = Request.QueryString["shop"]; // check for an error first if (!String.IsNullOrEmpty(error)) { this.TempData["Error"] = error; return(RedirectToAction("Login")); } // make sure we have the code if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(shop)) { return(RedirectToAction("Index", "Home")); } // get the authorization state ShopifyAuthorizationState authState = authorizer.AuthorizeClient(code); if (authState != null && authState.AccessToken != null) { using (ShopifyAppsContext context = new ShopifyAppsContext()) { var app = context.ShopifyApps.Find(new Guid(appGuid)); if (app != null) { app.ShopName = shopName; app.ApiKey = apiKey; app.SharedSecret = sharedSecret; app.AccessToken = authState.AccessToken; context.SaveChanges(); } } } return(RedirectToAction("Index")); }
public ActionResult Give(string id) { //string referrer = Request.UrlReferrer.ToString(); string qs = Request.QueryString.ToString(); // pass the supplied JSON Data Translator //var api = new ShopifyAPIClient((ShopifyAuthorizationState)Session["authState"], new JsonDataTranslator()); ShopifyAuthorizationState authState = Truffle.Shopify.ShopifyAuthorize.GetAuthorizationState(this.HttpContext); var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); var model = api.Get("/admin/products/" + id + ".json"); return(View("Give", model)); //return View("Give", referrer); //return View("Give", new { product = new { @id = id }, qs = qs }); }
public ProductDataSource() { // TODO: Of course it's a bad idea to hardcode this. In fact having this in your app is a bad idea period. This really should be exposed via // your own custom REST API that then connected to Shopify's. // This is just an example to show that it can be done, not how it *should* be done. ShopifyAuthorizationState authState = new ShopifyAuthorizationState { ShopName = "buckridge-hyatt6895", AccessToken = "42d4917ed3507278c748046addb01f3d" }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); // The JSON Data Translator will automatically decode the JSON for you var result = api.Get("/admin/products.json"); result.Completed = delegate(IAsyncOperation <dynamic> asyncAction, AsyncStatus asyncStatus) { var data = asyncAction.GetResults(); var products = data.products; foreach (var product in products) { var group = new ProductDataGroup(product.id.ToString(), product.title.ToString(), product.vendor.ToString(), product.images.Count > 0 ? product.images[0].src.ToString() : "", product.body_html.ToString()); var imgCount = 0; foreach (var variant in product.variants) { group.Items.Add(new ProductDataItem(variant.id.ToString(), variant.option1.ToString(), variant.option2.ToString(), imgCount < product.images.Count ? product.images[imgCount++].src.ToString() : "", "", group.Description, group)); } this.AllGroups.Add(group); } }; }
public static void SetAuthorization(System.Web.HttpContextBase httpContext, ShopifyAuthorizationState state) { httpContext.Session[AuthSessionKey] = state; try { Logger.Log("Setting auth state"); Graph.Instance.Cypher .Merge("(auth:ShopifyAuth { ShopName:{sn}, AccessToken:{at} })") .WithParam("sn", state.ShopName) .WithParam("at", state.AccessToken) .ExecuteWithoutResults(); } catch (Exception ex) { Logger.Log(ex.ToString()); } }
public ActionResult ShopifyAuthCallback(string code, string shop) { if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(shop)) { return(RedirectToAction("Index", "Home")); } shop = shop.Replace(".myshopify.com", String.Empty); ShopifyAuthClient client = new ShopifyAuthClient(shop, ConfigurationManager.AppSettings["Shopify.ConsumerKey"], ConfigurationManager.AppSettings["Shopify.ConsumerSecret"]); ShopifyAuthorizationState authState = client.ProcessAuthorization(); if (authState != null && authState.AccessToken != null) { this.Session["shopify_auth_state"] = authState; } return(RedirectToAction("Index", "Home")); }
public dynamic GetEntity(string entity, string id, string token, string shopName, string[] fields, int top) { var authState = new ShopifyAuthorizationState { AccessToken = token, ShopName = shopName }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); var queryString = ""; dynamic entityObject; if (fields.Length != 0) { var commaSeparatedFields = string.Join(",", fields); queryString = "fields=" + commaSeparatedFields + "&"; } if (entity.Equals("collections")) { entity = "products"; entityObject = GetEntities(entity, token, shopName, fields, top, id); return(entityObject); } if (entity.Equals("customer_saved_searches")) { id = id + "/customers"; if (top != 0) { queryString = queryString + "limit=" + top; } } var urlSuffix = $"/admin/{entity}/{id}.json?{queryString}"; entityObject = (JObject)api.Get(urlSuffix); return(entityObject); }
// // GET: /Shopify/Test public ActionResult Test() { var appID = Request.Params["id"]; using (ShopifyAppsContext context = new ShopifyAppsContext()) { var app = context.ShopifyApps.Find(new Guid(appID)); ViewData["App"] = app; ShopifyAuthorizationState authState = new ShopifyAuthorizationState { ShopName = app.ShopName, AccessToken = app.AccessToken }; var api = new ShopifyAPIClient(authState, new JsonDataTranslator()); // The JSON Data Translator will automatically decode the JSON for you ViewData["ProductData"] = api.Get("/admin/products.json"); } return(View()); }
public ActionResult DoAuth(string code = "", string shop = "", string error = "") { // get the following variables from the Query String of the request //string code = ""; //string shop = ""; //string error = ""; // check for an error first if (!String.IsNullOrEmpty(error)) { this.TempData["Error"] = error; return(RedirectToAction("Login")); } // make sure we have the code if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(shop)) { return(RedirectToAction("index", "home")); } var shopName = shop.Replace(".myshopify.com", String.Empty); var authorizer = new ShopifyAPIAuthorizer(shopName, ConfigurationManager.AppSettings["Shopify.ConsumerKey"], // In this case I keep my key and secret in my config file ConfigurationManager.AppSettings["Shopify.ConsumerSecret"]); // get the authorization state ShopifyAuthorizationState authState = authorizer.AuthorizeClient(code); if (authState != null && authState.AccessToken != null) { // TODO! // store the auth state in the session or DB to be used for all API calls for the specified shop Session["authState"] = authState; } return(View("Success")); }
protected void Application_AcquireRequestState(object sender, EventArgs e) { if (Request.Url.PathAndQuery.Contains("Authorize.aspx")) { return; } // ignore non .net files if (!Request.FilePath.EndsWith(".aspx")) { return; } if (HttpContext.Current.Session == null) { Response.Redirect("Authorize.aspx"); } // if we are not authenticated, go to Authorize page if (HttpContext.Current.Session["Shopify.AuthState"] == null) { Response.Redirect("Authorize.aspx"); } ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState; if (state == null) { Response.Redirect("Authorize.aspx"); } if (String.IsNullOrEmpty(state.AccessToken)) { Response.Redirect("Authorize.aspx"); } }
public async Task <ActionResult> ShopifyAuthCallback(string code, string shop, string error) { if (!String.IsNullOrEmpty(error)) { this.TempData["Error"] = error; return(RedirectToAction("Login")); } if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(shop)) { return(RedirectToAction("Index", "Home")); } var shopName = shop.Replace(".myshopify.com", String.Empty); var authorizer = new ShopifyAPIAuthorizer(shopName, ConfigurationManager.AppSettings["Shopify.ConsumerKey"], ConfigurationManager.AppSettings["Shopify.ConsumerSecret"]); ShopifyAuthorizationState authState = await authorizer.AuthorizeClient(code); if (authState != null && authState.AccessToken != null) { Shopify.ShopifyAuthorize.SetAuthorization(this.HttpContext, authState); } return(RedirectToAction("Index", "Home")); }
/// <summary> /// Creates an instance of this class for use with making API Calls /// </summary> /// <param name="state">the authorization state required to make the API Calls</param> public ShopifyAPIClient(ShopifyAuthorizationState state) { this.State = state; }
/// <summary> /// /// </summary> /// <param name="sessionState"></param> /// <param name="state"></param> public static void SetAuthorization(System.Web.HttpContextBase httpContext, ShopifyAuthorizationState state) { httpContext.Session[AuthSessionKey] = state; }
public void ShouldCreateProductUri() { var authState = new ShopifyAuthorizationState() {AccessToken = "beep boop", ShopName = "chucks-chili-dogs"}; var sapi = new ShopifyAPIContext(authState); Assert.AreEqual("/admin/products/67", sapi.ProductPath("67")); }
/// <summary> /// Creates an instance of this class for use with making API Calls /// </summary> /// <param name="state">the authorization state required to make the API Calls</param> /// <param name="translator">the translator used to transform the data between your C# client code and the Shopify API</param> public ShopifyAPIClient(ShopifyAuthorizationState state, IDataTranslator translator) { this.State = state; this.Translator = translator; }