Shopify访问令牌返回null

时间:2018-07-27 02:04:51

标签: c# shopify restsharp shopify-app

在旅程即将结束时,我意识到我的访问令牌即将失效。我在Visual Studio中使用带有C#的RectSharp来启动和运行shopify应用,一切正常,直到按Install,然后再回到Visual Studio,并告诉accessToken为null D:

这是一些有关错误和调试的图片 click here for image

这是我注意到在r周围弹出的错误,它是用rectSharp写的,对我来说看起来是正确的。

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

这是我的验证码:

 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();
        }
    }

1 个答案:

答案 0 :(得分:3)

请尝试通过如下更改request.AddHeaderrequest.AddParameter

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);
相关问题