· 7 years ago · Jul 25, 2018, 11:06 AM
1public ActionResult install(string shop, string signature, string timestamp)
2 {
3 string r = string.Format("https://{0}/admin/oauth/authorize?client_id={1}&scope=read_fulfillments,write_fulfillments&redirect_uri=https://{2}/fulfillment/auth", shop, apiKey, appUrl);
4 return Redirect(r);
5 }
6
7
8
9 public ActionResult auth(string shop, string code)
10 {
11 //Vertify if from shopify
12
13 if (IsAuthenticRequest(Request.QueryString, secretKey))
14 {
15 string u = string.Format("https://{0}/admin/oauth/access_token", shop);
16
17 var client = new RestClient(u);
18
19 var request = new RestRequest(Method.POST);
20
21 request.RequestFormat = DataFormat.Json;
22 request.AddHeader("Content-Type", "application/json");
23
24 request.AddParameter("application/x-www-form-urlencoded", "client_id=" + apiKey + "&client_secret=" + secretKey + "&code=" + code, ParameterType.RequestBody);
25
26 var response = client.Execute(request);
27
28 var r = JsonConvert.DeserializeObject<dynamic>(response.Content);
29 var accessToken = r.access_token;
30
31 // save the shop Token!
32 SaveShopToken(shop, (string)accessToken);
33 }
34
35
36 return View();
37 }
38
39 private void SaveShopToken(string shop, string token)
40 {
41 using (var con = new fulfillmentdbEntities ())
42 {
43 //Check if this is a new shop
44 if(con.ShopTokens.Any(c=>c.Shop == shop))
45 {
46 // if shop exists, than update
47
48 } else
49 {
50 //then if its not existing, its new!
51 con.ShopTokens.Add(new ShopToken { Shop = shop, Token = token, InstallDate = DateTime.Now });
52
53 }
54
55 con.SaveChanges();
56 }
57 }