使用 Django 在 Shopify 应用程序中卸载后出现错误

问题描述 投票:0回答:1

我正在参考 https://github.com/Shopify/shopify_django_app 存储库来制作 Shopify 应用程序。问题是应用程序安装正确并且工作正常,但问题是如果我们在从shopify商店卸载应用程序后访问应用程序URL,它会返回以下error.

我相信该错误是由于 shopify_app.decorator.py 文件中的“shop_login_required”装饰器造成的。该装饰器用于 home.views.py 文件中的登录视图。

完整错误日志:-

Internal Server Error: /
Traceback (most recent call last):
  File "D:\app\venv\lib\site-packages\pyactiveresource\connection.py", line 286, in _open
    http_response = self._handle_error(self._urlopen(request))
  File "D:\app\venv\lib\site-packages\pyactiveresource\connection.py", line 316, in _urlopen
    return urllib.request.urlopen(request, timeout=self.timeout)
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 525, in open
    response = meth(req, response)
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 634, in http_response
    response = self.parent.error(
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 563, in error
    return self._call_chain(*args)
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\royal\miniconda3\lib\urllib\request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\app\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "D:\app\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\app\shopify_django_app\shopify_app\decorators.py", line 12, in wrapper
    return fn(request, *args, **kwargs)
  File "D:\app\shopify_django_app\home\views.py", line 16, in index
    products = shopify.Product.find()
  File "D:\app\venv\lib\site-packages\shopify\base.py", line 196, in find
    collection = super(ShopifyResource, cls).find(id_=id_, from_=from_, **kwargs)
  File "D:\app\venv\lib\site-packages\pyactiveresource\activeresource.py", line 386, in find
    return cls._find_every(from_=from_, **kwargs)
  File "D:\app\venv\lib\site-packages\pyactiveresource\activeresource.py", line 525, in _find_every
    response = cls.connection.get(path, cls.headers)
  File "D:\app\venv\lib\site-packages\pyactiveresource\connection.py", line 329, in get
    return self._open('GET', path, headers=headers)
  File "D:\app\venv\lib\site-packages\shopify\base.py", line 23, in _open
    self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
  File "D:\app\venv\lib\site-packages\pyactiveresource\connection.py", line 288, in _open
    http_response = self._handle_error(err)
  File "D:\app\venv\lib\site-packages\pyactiveresource\connection.py", line 415, in _handle_error
    raise UnauthorizedAccess(err)
pyactiveresource.connection.UnauthorizedAccess: Response(code=401, body="b'{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}'", headers={'Date': 'Fri, 11 Aug 2023 11:46:01 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'close', 'X-Sorting-Hat-PodId': '319', 'X-Sorting-Hat-ShopId': '72702755136', 'Referrer-Policy': 'origin-when-cross-origin', 'X-Frame-Options': 'DENY', 'X-ShopId': '72702755136', 'X-ShardId': '319', 'WWW-Authenticate': 'Basic Realm="Shopify API Authentication"', 'Strict-Transport-Security': 'max-age=7889238', 'Server-Timing': 'processing;dur=29', 'X-Shopify-Stage': 'production', 'Content-Security-Policy': "default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopifycdn.net https://checkout.shopifycs.com https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=b46c2afc-80f3-4566-90e3-0fe20c35d3ba", 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Permitted-Cross-Domain-Policies': 'none', 'X-XSS-Protection': '1; mode=block; report=/xss-report?source%5Baction%5D=index&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=b46c2afc-80f3-4566-90e3-0fe20c35d3ba', 'X-Dc': 'gcp-asia-south1,gcp-us-central1,gcp-us-central1', 'X-Request-ID': 'b46c2afc-80f3-4566-90e3-0fe20c35d3ba', 'CF-Cache-Status': 'DYNAMIC', 'Report-To': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=5ia78nHKzIhToOvDIQVkvrHwaxDNNvCQXSPxnqHQZ0n%2FosdCZTI2E9ACFETWfr0eyz4%2Bu7pi9JAdJWJCz5I4oMnj0AbooqopOxi5bxA%2B%2FhMkGOs2ivWCsIQfja10K7EnBQyEg%2BKb%2FuBsuw%3D%3D"}],"group":"cf-nel","max_age":604800}', 'NEL': '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}', 'Server': 'cloudflare', 'CF-RAY': '7f503c547e451798-MAA', 'alt-svc': 'h3=":443"; ma=86400'}, msg="Unauthorized")```
python django shopify shopify-app shopify-api
1个回答
0
投票

这个问题就解决了。我在应用程序安装时创建了一个应用程序/卸载 webhook。

def create_uninstall_webhook(shop_name, access_token):
    with shopify_session(shop_name, access_token):
        app_url = apps.get_app_config("shopify_app").APP_URL
        webhook = shopify.Webhook()
        webhook.topic = "app/uninstalled"
        webhook.address = app_url + '/shopify/uninstall/'
        webhook.format = "json"
        webhook.save()
def shopify_session(shopify_domain, access_token):
    api_version = apps.get_app_config("shopify_app").SHOPIFY_API_VERSION

    return shopify.Session.temp(shopify_domain, api_version, access_token)

def finalize(request):
    ...
    ...
    create_uninstall_webhook(shop_name, access_token)
    ...
    ...

除此之外,如果有人不想这样做,shopify 会要求我们在合作伙伴的仪表板中订阅强制 webhooks。强制 Webhook 之一是“应用程序卸载”,它会在应用程序卸载后 48 小时内通知我们,我们可以相应地执行强制操作。

© www.soinside.com 2019 - 2024. All rights reserved.