Created
March 24, 2012 19:28
-
-
Save cmcdonaldca/2186993 to your computer and use it in GitHub Desktop.
Using Rob Conery's C# Shopify API Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-download the c# wrapper: https://github.com/robconery/ShopifyApi | |
-to get it to build I had to install MVC3 (http://www.asp.net/mvc/mvc3). | |
I was having a warning about a System.Web.Helpers and MVC3 solved that | |
-then I created a Web Project and added it to my solution. Added the | |
Shopify project as a reference to that new web application project. | |
-Looks at my sample Default.aspx page to see how I listed the products in | |
the store. In Rob Conery's example, I had to change "var" to "dynamic" | |
in order to get the project to build. | |
-you will have to manually install the app with the proper URL and API KEY | |
(GET <shop url>/admin/api/auth?api_key=<API key>) as noted here: | |
http://api.shopify.com/authentication.html | |
-For my basic example... this is not a recommendation for how to setup | |
an app. I played with this for about 30min and wasn't concerned with | |
form yet. I'm only posting this up to help #HackVan | |
-NOTE:In my example, in order to print the title of a product | |
I had to cast my object to Shopify.JsonHelper.DynamicJsonObject. | |
In order to do this I had to go into the C# API Wrapper and make | |
that class public. This may or may not be the best way to do this. | |
I didn't spend anytime yet to figure out the best approach for this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" | |
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> | |
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> | |
</asp:Content> | |
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> | |
<asp:ListView ID="Products" ItemPlaceholderID="items" runat="server"> | |
<LayoutTemplate> | |
<ul> | |
<asp:PlaceHolder ID="items" runat="server"></asp:PlaceHolder> | |
</ul> | |
</LayoutTemplate> | |
<ItemTemplate> | |
<li><%# GetTitle(Container.DataItem) %></li> | |
</ItemTemplate> | |
</asp:ListView> | |
</asp:Content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Security.Cryptography; | |
using Shopify; | |
namespace WebApplication1 | |
{ | |
public partial class _Default : System.Web.UI.Page | |
{ | |
protected string shop | |
{ | |
get | |
{ | |
if (Session["shop"] == null) | |
{ | |
if (String.IsNullOrEmpty(Request.QueryString["shop"])) | |
{ | |
throw new ApplicationException("Cannot find shop info");// install in shop and click link from shop | |
} | |
Session["shop"] = Request.QueryString["shop"]; | |
} | |
return (string)Session["shop"]; | |
} | |
} | |
protected string token | |
{ | |
get | |
{ | |
if (Session["token"] == null) | |
{ | |
if (String.IsNullOrEmpty(Request.QueryString["t"])) | |
{ | |
throw new ApplicationException("Cannot find token info");// install in shop and click link from shop | |
} | |
Session["token"] = Request.QueryString["t"]; | |
} | |
return (string)Session["token"]; | |
} | |
} | |
protected string signature | |
{ | |
get | |
{ | |
if (Session["signature"] == null) | |
{ | |
if (String.IsNullOrEmpty(Request.QueryString["signature"])) | |
{ | |
throw new ApplicationException("Cannot find signature info");// install in shop and click link from shop | |
} | |
Session["signature"] = Request.QueryString["signature"]; | |
} | |
return (string)Session["signature"]; | |
} | |
} | |
protected string timestamp | |
{ | |
get | |
{ | |
if (Session["timestamp"] == null) | |
{ | |
if (String.IsNullOrEmpty(Request.QueryString["timestamp"])) | |
{ | |
throw new ApplicationException("Cannot find timestamp info");// install in shop and click link from shop | |
} | |
Session["timestamp"] = Request.QueryString["timestamp"]; | |
} | |
return (string)Session["timestamp"]; | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
string api_key = "TODO"; | |
string secret = "REPLACE"; | |
bool isAuthorized = String.Compare(md5(String.Format("{0}shop={1}t={2}timestamp={3}", secret, shop, token, timestamp)), signature) == 0; | |
if (!isAuthorized) | |
{ | |
throw new ApplicationException("not authorized"); | |
} | |
string password = md5(String.Format("{0}{1}", secret, token)); | |
dynamic shopify = new Shopify.Api(api_key, password, shop); | |
var query = shopify.Products(); | |
Products.DataSource = query.products; | |
Products.DataBind(); | |
} | |
protected string md5(string input) | |
{ | |
using (MD5 md5Hash = MD5.Create()) | |
{ | |
// Convert the input string to a byte array and compute the hash. | |
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); | |
// Create a new Stringbuilder to collect the bytes | |
// and create a string. | |
StringBuilder sBuilder = new StringBuilder(); | |
// Loop through each byte of the hashed data | |
// and format each one as a hexadecimal string. | |
for (int i = 0; i < data.Length; i++) | |
{ | |
sBuilder.Append(data[i].ToString("x2")); | |
} | |
// Return the hexadecimal string. | |
return sBuilder.ToString(); | |
} | |
} | |
protected string GetTitle(object obj) | |
{ | |
dynamic prod = (Shopify.JsonHelper.DynamicJsonObject)obj; | |
return prod.title; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment