Search code examples
c#shopifyrestsharpshopify-app

Shopify access Token returning null


Near the end of my journey I realized that my access Token was coming up null. Im using RectSharp in visual studio with C# to get a shopify app up and running, everything works up to pressing install, where then I get brought back to visual studio and told that the accessToken is null D:

Here is some pictures of the errors and my debuging click here for image

this is the error I noticed poping up around r, which is written in rectSharp, and looks correct to me.

{{
  "error": "822: unexpected token at 'client_id=6e83a9e8b79dccbad77530c7273e3e00&client_secret=03cfa0ef9b451868bb0160e4ad75e0ec&code=3a941fe03d9fc0ea0d4226f9f4af316f'"
}}

here is my auth code:

 public ActionResult install(string shop, string signature, string timestamp)
    {
        string r = string.Format("https://{0}/admin/oauth/authorize?client_id={1}&scope=read_fulfillments,write_fulfillments,read_orders,write_products&redirect_uri=https://{2}/fulfillment/auth", shop, apiKey, appUrl);
        return Redirect(r);
    }

    public ActionResult auth(string shop, string code)
    {

            string u = string.Format("https://{0}/admin/oauth/access_token", shop);

            var client = new RestClient(u);

            var request = new RestRequest(Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content-Type", "application/json");

            request.AddParameter("application/x-www-form-urlencoded", "client_id=" + apiKey + "&client_secret=" + secretKey + "&code=" + code, ParameterType.RequestBody);

            var response = client.Execute(request);

            var r = JsonConvert.DeserializeObject<dynamic>(response.Content);
            var accessToken = r.access_token;
            accesstokenmain = r.access_token;
            //save shop token and information to database
            SaveShopToken(shop, (string)accessToken);

            //Part 5
            //create a un-install web hook
            //you want to know when customers delete your app from their shop

            string unInstallUrlCallback = "https://8a047f39.ngrok.io/fulfillment/uninstall";

            string shopAdmin = string.Format("https://{0}/admin/", shop);

            var webHook = new WebHookBucket();
            webHook.Whook = new WebHook { Format = "json", Topic = "app/uninstalled", Address = unInstallUrlCallback };

            CreateUninstallHook(shopAdmin, "webhooks.json", Method.POST, (string)accessToken, webHook);

            return Redirect(AskCustomerCharge(shop, (string)accessToken));


       // return View();
    }

    public void SaveShopToken(string shop, string token)
    {
        using (var con = new fulfillmentdbEntities())
        {
            if (con.ShopTokens.Any(c=>c.Shop == shop))
            {

            } else
            {
                con.ShopTokens.Add(new ShopToken { Shop = shop, Token = token, InstallDate = DateTime.Now });
            }

            con.SaveChanges();
        }
    }

Solution

  • Please try by changing the request.AddHeader and request.AddParameter as below

    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded", $"client_id={apiKey}&client_secret={secretKey}&code={code}, ParameterType.RequestBody);