在 Altair GraphQL Client 中请求 Shopify’s GraphQL Admin API
1、参考:https://shopify.dev/api/admin-graphql#authentication ,所有 GraphQL Admin API 查询都需要有效的 Shopify 访问令牌。
2、打开后台 – 应用 – 开发应用 – 创建自定义应用。如图1
3、输入应用名称,选择应用开发者,创建应用。如图2
4、配置后台 API 范围。如图3
5、应用开发 – Shopify 访问令牌 – 配置 – 后台 API 集成 – 选择访问范围 – 保存。如图4
6、API 凭据,您还没有任何访问令牌。若要获取后台 API 访问令牌,请安装应用。安装应用。如图5
7、API 凭据 – 后台 API 访问令牌。为保护您的数据,您的后台 API 令牌只能显示一次。请复制您的后台 API 访问令牌并将其保存在安全位置。如图6
8、参考:https://shopify.dev/api/admin-graphql#endpoints 。通过向端点发送 POST HTTP 请求来执行 GraphQL 查询。获取最近添加的三个产品的 ID 和标题。在 Altair GraphQL Client 中,新开窗口,输入网址:https://xxx.myshopify.com/admin/api/2022-01/graphql.json ,弹出登录框,如图7
9、设置 Headers ,Content-Type: application/graphql,X-Shopify-Access-Token: {access_token}。如图8
10、设置查询后,响应 404。查看网络请求,先是 301 跳转至 /admin/auth/login,然后 302 跳转至 /admin。如图9
{ products(first: 3) { edges { node { id title } } } }
11、但是在 Git Bash 执行查询,响应 200。如图10
$ curl -X POST \ > https://xxx.myshopify.com/admin/api/2022-01/graphql.json \ > -H 'Content-Type: application/graphql' \ > -H 'X-Shopify-Access-Token: shpat_' \ > -d ' > { > products(first: 3) { > edges { > node { > id > title > } > } > } > } > ' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 621 0 503 100 118 493 115 0:00:01 0:00:01 --:--:-- 610{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/7560274542827","title":"满送优惠测试"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560295743723","title":"商品A"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560318976235","title":"Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}
12、参考: https://shopify.dev/api/admin/getting-started#use-curl 。如果您使用的是 HTTP 客户端,例如 Postman 或 Insomnia,则必须将 Content-Type 设置为 application/json 而不是 application/graphql。重新设置 Headers ,Content-Type: application/json。仍然响应 404。当切换为一个正式的商店后,响应 200。如图11
{ products(first: 3) { edges { node { id title } } } }
{ "data": { "products": { "edges": [ { "node": { "id": "gid://shopify/Product/6944715342030", "title": "commercial kitchen quality" } }, { "node": { "id": "gid://shopify/Product/6944715636942", "title": "Easter Photographing Dress-up Acessories" } }, { "node": { "id": "gid://shopify/Product/6944715702478", "title": "Perfect Pet Hair Remover" } } ] } }, "extensions": { "cost": { "requestedQueryCost": 5, "actualQueryCost": 5, "throttleStatus": { "maximumAvailable": 1000, "currentlyAvailable": 995, "restoreRate": 50 } } } }
13、当 设置 Headers ,Content-Type: application/graphql 时,报错:Parse error on \”query\” (STRING) at [1, 2]。如图12
{ "errors": [ { "message": "Parse error on \"query\" (STRING) at [1, 2]", "locations": [ { "line": 1, "column": 2 } ] } ] }
14、在 Postman 中,二个商店皆是未跳转的,试用期响应 200,正式响应 200。试用期可用,正式可用。符合预期。如图12
试用期商店的响应
{ "data": { "products": { "edges": [ { "node": { "id": "gid://shopify/Product/7560274542827", "title": "满送优惠测试" } }, { "node": { "id": "gid://shopify/Product/7560295743723", "title": "商品A" } }, { "node": { "id": "gid://shopify/Product/7560318976235", "title": "Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机" } } ] } }, "extensions": { "cost": { "requestedQueryCost": 5, "actualQueryCost": 5, "throttleStatus": { "maximumAvailable": 1000.0, "currentlyAvailable": 995, "restoreRate": 50.0 } } } }
正式商店的响应
{ "data": { "products": { "edges": [ { "cursor": "eyJsYXN0X2lkIjo2OTQ0NzE1MzQyMDMwLCJsYXN0X3ZhbHVlIjoiNjk0NDcxNTM0MjAzMCJ9", "node": { "id": "gid://shopify/Product/6944715342030", "title": "commercial kitchen quality" } } ], "pageInfo": { "hasNextPage": true, "hasPreviousPage": false } } }, "extensions": { "cost": { "requestedQueryCost": 3, "actualQueryCost": 3, "throttleStatus": { "maximumAvailable": 1000.0, "currentlyAvailable": 997, "restoreRate": 50.0 } } } }
15、在 Git Bash 中,试用期可用,正式可用。符合预期。
试用期商店的响应
$ curl -X POST https://xxx.myshopify.com/admin/api/2022-01/graphql.json -H 'Content-Type: application/graphql' -H 'X-Shopify-Access-Token: shpat_' -d ' { products(first: 3) { edges { node { id title } } } } ' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 621 0 503 100 118 479 112 0:00:01 0:00:01 --:--:-- 593{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/7560274542827","title":"满送优惠测试"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560295743723","title":"商品A"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560318976235","title":"Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}
正式商店的响应
$ curl -X POST \ > https://fuyan666.myshopify.com/admin/api/2022-01/graphql.json \ > -H 'Content-Type: application/graphql' \ > -H 'X-Shopify-Access-Token: shpat_' \ > -d ' > { > products(first: 3) { > edges { > node { > id > title > } > } > } > } > ' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 604 0 486 100 118 538 130 --:--:-- --:--:-- --:--:-- 670{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/6944715342030","title":"commercial kitchen quality"}},{"node":{"id":"gid:\/\/shopify\/Product\/6944715636942","title":"Easter Photographing Dress-up Acessories"}},{"node":{"id":"gid:\/\/shopify\/Product\/6944715702478","title":"Perfect Pet Hair Remover"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}
16、在 Postman 、Git Bash 中,试用期与正式皆可用。而在 Altair GraphQL Client,试用期不可用,仅正式可用。初步怀疑与在试用期网址调用时手动输入帐号密码登录有关系。不应手动登录。
2 条回复
[…] 上一篇 在 Altair GraphQL Client 中请求 Shopify’s GraphQL Admin API […]
[…] 在 Postman 中请求 Shopify’s GraphQL Admin API – 永夜发表在《在 Altair GraphQL Client 中请求 Shopify’s GraphQL Admin API》 […]