javascript - Shopify 验证失败
问题描述
我正在尝试在 Shopify 中创建一个公共应用程序。我可以设置我们的应用程序所需的范围。在安装过程中,它要求进行 shopify 验证。我已参考此文档https://shopify.dev/tutorials/authenticate-with-oauth#verification。我已将消息传递为 code=%code-from-shopify%&shop=%shop.myshopify.com%&state=%state-from-shopify%×tamp=%timestamp-from-shopify% 作为 Shopify 应用程序的密钥,但它从不与作为参数存在于 url 中的 hmac 匹配。
我在 3 年前创建了一个应用程序,正如我上面提到的,它运行良好,但是当我 9 天前创建一个应用程序时无法运行,并且这个新应用程序的密钥以 shpss_ 开头
我该如何解决?
解决方案
- 首先请检查您的 SHOPIFY_KEY 和 SHOPIFY_SECRET 是否正确,您是否在代码中使用相同的。
- 从 shopify 重定向到给定的回调 URL 后,您将从请求中获得 hmac 和代码。
这是从 hmac 获取访问令牌的代码
foreach ($_REQUEST as $key => $value) {
if ($key !== "hmac" && $key != "signature") {
$hashArray[] = $key . "=" . $value;
}
}
$hashString = implode($hashArray, "&");
$hashedString = hash_hmac("sha256", $hashString, 'YOUR_SHOPIFY_SECRET');
/* compare resulting hashed string with hmac parameter */
if ($_REQUEST['hmac'] !== $hashedString) {
return 403;
}
$shopUrl = "https://" . $_REQUEST["shop"] . "/admin/oauth/access_token.json";
$postData = http_build_query(
array(
"client_id" => 'YOUR_SHOPIFY_KEY',
"client_secret" => 'YOUR_SHOPIFY_SECRET',
"code" => $_REQUEST["code"],
)
);
/* Call using curl post you will get Access token in JSON */
$result = shop_auth_curl_request_call($shopUrl, $postData);
$tokenResponse = json_decode($result, true);
$tokenResponse['access_token'] // In JSON you will get access token
推荐阅读
- javascript - 在 React 组件中添加 JavaScript 代码并调用它
- python-3.x - 绘制具有多个类别的掩码,并省略背景(matplotlib)
- r - 在日志空间中将 P 转换为 Z
- javascript - MAC M1 安装“sharp”模块出现问题
- c# - for循环中的C#ADO.Net事务不插入所有记录而只插入一条记录
- sms - Jasmin SMPP 服务器绑定问题:ESME_RBINDFAIL ({'name': 'ESME_RBINDFAIL', 'description': 'Bind Failed'})
- python - 如何使用python编辑.txt文件,我想在每个数字后粘贴一个特定的字符串
- python - 每 2 分钟在 Web 服务器上执行一个脚本/文件(Python、.exe)
- c++ - 视觉工作室 C++ 上的 strchrnul?
- r - 具有错误维度的混淆矩阵