我正在尝试通过基于标签的过滤从 shopify 获取产品。标签将是动态的,不止一个,并且会发生变化。
import json
import time
import requests
API_KEY = 'xxxx'
PASSWORD = 'xxxx'
SHOP_NAME = 'xxxx'
API_VERSION = '2020-04' #change to the API version
shop_url = "https://%s:%s@%s.myshopify.com/admin/api/%s" % (API_KEY, PASSWORD, SHOP_NAME, API_VERSION)
def callShopifyGraphQL(GraphQLString, data):
headers = {
"X-Shopify-Storefront-Access-Token": 'xxxxxx',
"accept":"application/json"
}
response = requests.post(shop_url+'/graphql', json={'query': GraphQLString, 'variables': data}, headers=headers)
answer = json.loads(response.text)
return answer['data']
str1 = '0-12'
str2 = 'physical'
graphQLquery7 = """ {
products(first:100, query:"tag:$tags") {
edges {
node {
id
tags
title
onlineStoreUrl
}
}
}
}"""
tag = dict({
"tags":[str1,str2]
})
resp = callShopifyGraphQL(graphQLquery7, tag)
print json.dumps(resp)
# This query works fine and gives multiple products
# graphQLquery2 = """{
# products(first:100, query:"tag:[0-12, physical]") {
# edges {
# cursor
# node {
# id
# tags
# title
# onlineStoreUrl
# }
# }
# }
# }"""
我得到的输出基本上是一个产品为空的 JSON
{u'extensions': {u'cost': {u'requestedQueryCost': 102, u'throttleStatus': {u'restoreRate': 50.0, u'currentlyAvailable': 998, u'maximumAvailable': 1000.0}, u'actualQueryCost': 2}}, u'data': {u'products': {u'edges': []}}}
{"products": {"edges": []}}
我无法在查询中将我的标签作为变量传递。我目前正在使用 GraphQl,因为我找不到 REST API 获取基于多个标签的产品,这些标签会有所不同。
编辑:删除了 Python 标记,因为这不是 Python 问题,并且我已经添加了答案以及关于如何执行此操作的两种方法