WooCommerce API 在 Google App Engine 中给出 401,但在本地环境中工作(200 OK)

WooCommerce API gives 401 in Google App Engine but works (200 OK) in local env

我正在编写一个应用程序,允许用户将他们的 WooCommerce 商店与我的网站集成。我正在使用 Python wrapper for the WooCommerce REST API,我的应用程序托管在 Google App Engine 上。这是我调用 Woocommerce API 的代码。 (我正在使用 https 连接到所有 WooCommerce 商店)

 def request_woo_api(self, user, request_url):
        wcapi = API(
            url=user.woo_url,
            consumer_key=user.woo_consumer_key,
            consumer_secret=user.woo_consumer_secret,
            wp_api=True,
            version="wc/v2",
            query_string_auth=True
            )
        resp = wcapi.get(request_url).json()
        return resp

当我在我的开发机器上测试我的代码时,我得到了 200 OK 响应。但是当我将我的代码部署到 Google App Engine 时,我收到 一些 WooCommerce 商店的 401 响应。

{u'message': u'Sorry, you cannot list resources.', u'code': u'woocommerce_rest_cannot_view', u'data': {u'status': 401}}

我在本文档中读到 here Google App Engine 为所有出站请求附加了一个值 (AppEngine-Google; (http://code.google.com/appengine; appid: <application_id>)) 到 user-agent header。我认为这是问题所在,因为我尝试从我的本地计算机调用 woocommerce api 并且它有效,但是当我将此值附加到我的 header 时,相同的端点给出了 401 错误。

>>> headers = {'User-Agent': 'WooCommerce API Client-Python/1.2.1'} 
>>> result = requests.get(url, headers=headers) 
>>> print result.json() 
[{u'date_completed_gmt': None, u'date_modified_gmt': u'2017-11-30T18:13:59', u'payment_method': u'.......
>>> headers = {'User-Agent': 'WooCommerce API Client-Python/1.2.1; AppEngine-Google; (http://code.google.com/appengine; appid: 123)'} 
>>> result = requests.get(url, headers=headers) 
>>> print result.json() 
{u'message': u'Sorry, you cannot list resources.', u'code': u'woocommerce_rest_cannot_view', u'data': {u'status': 401}}

事实上,这种情况只发生在几个 WooCommerce 网站上,而不是其他网站,这让我认为这些用户可能有 WooCommerce setting/plugin extension/security 设置导致了这个问题。有什么办法-

  1. 阻止 GAE 将此 header 附加到出站请求?
  2. 关闭阻止 WooCoomerce 商店根据 user-agent 值过滤请求的插件或设置? (我可以要求用户更改他们站点的安全设置)

请帮忙。

( 我已经尝试了以下方法,但没有帮助:

更新

经过进一步调查,我发现 WooCommerce 商店服务器忽略了查询字符串参数。
如果我不发送“query_string_auth=true”设置,我就能收到回复。但是商店会忽略所有查询参数。 query_string_auth 参数在查询字符串中发送 consumer_key 和密码。当我删除它时,它会作为 auth header。现在我将其作为 auth header 发送,它会响应。但它忽略了查询参数。我的猜测是服务器忽略了所有查询参数。这解释了为什么我首先得到 401。 (因为我们将密钥和机密作为查询参数发送)。这只会在我发送 "User-Agent: AppEngine-Google;"

时发生

示例:

store_url = "https://teststore.com"
endpoint = "/wp-json/wc/v2/orders?per_page=1"
auth = ("ck_xxxxxxxxxxx", "cs_xxxxxxxxxxx")
headers = {'Content-Type': 'application/json', 'User-Agent': 'WooCommerce API Client-Python/1.2.1'}
url = '{}/{}'.format(store_url, endpoint)
response = requests.get(url, auth=auth, headers=headers)
print response.json()

这returns正好是一个订单。

store_url = "https://teststore.com"
endpoint = "/wp-json/wc/v2/orders?per_page=1"
auth = ("ck_xxxxxxxxxxx", "cs_xxxxxxxxxxx")
headers = {'Content-Type': 'application/json', 'User-Agent': 'WooCommerce API Client-Python/1.2.1; AppEngine-Google; (+http://code.google.com/appengine; appid: 123)'}
url = '{}/{}'.format(store_url, endpoint)
response = requests.get(url, auth=auth, headers=headers)
print response.json()

这returns 10个订单(默认页面大小为10)

但我仍然不知道为什么有些商店会这样做或如何解决这个问题。

终于找到了解决我的问题的方法。 导致问题的是 WP Engine 设置。我的应用托管在 Google App Engine 上,它添加了一个 header 值“AppEngine-Google; (+http://code.google.com/appengine; appid:” 到所有请求中的 User-Agent header。WP Engine 将来自机器人的请求重定向到页面的缓存版本。它通过解析 User-Agent header。当它在值中找到 'Google' 时,它假定它是一个 Google 机器人并将其重定向到缓存页面。因此,它最终忽略了所有查询URL 中的参数(凭证、过滤器参数等)。

我根本不需要更改我的代码。我只是联系了 WP-Engine 支持并要求他们为我的一个测试 WooCommerce 站点关闭 user-agent-based 机器人过滤器(默认情况下打开)并且它起作用了!