Shopify 访问令牌返回 null
Shopify access Token returning null
在旅程即将结束时,我意识到我的访问令牌即将变为空。我在 visual studio 中使用 RectSharp 和 C# 来启动 shopify 应用程序 运行,一切正常按安装,然后我回到 visual studio 并被告知 accessToken 为空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();
}
}
请尝试将 request.AddHeader
和 request.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);
在旅程即将结束时,我意识到我的访问令牌即将变为空。我在 visual studio 中使用 RectSharp 和 C# 来启动 shopify 应用程序 运行,一切正常按安装,然后我回到 visual studio 并被告知 accessToken 为空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();
}
}
请尝试将 request.AddHeader
和 request.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);