Python 请求使用变量向 graphql 发布查询

Posted

技术标签:

【中文标题】Python 请求使用变量向 graphql 发布查询【英文标题】:Python requests post a query to graphql with variables 【发布时间】:2021-07-08 00:29:25 【问题描述】:

我正在尝试获取在 opensea.io 上出售的商品的报价列表

def getHighestOffer(self):
    query = """query OrdersQuery(\n  $cursor: String\n  $count: Int = 10\n  $excludeMaker: IdentityInputType\n  $isExpired: Boolean\n  $isFilled: Boolean\n  $isValid: Boolean\n  $maker: IdentityInputType\n  $makerArchetype: ArchetypeInputType\n  $makerAssetIsPayment: Boolean\n  $takerArchetype: ArchetypeInputType\n  $takerAssetCategories: [CollectionSlug!]\n  $takerAssetCollections: [CollectionSlug!]\n  $takerAssetIsOwnedBy: IdentityInputType\n  $takerAssetIsPayment: Boolean\n  $sortAscending: Boolean\n  $sortBy: OrderSortOption\n  $makerAssetBundle: BundleSlug\n  $takerAssetBundle: BundleSlug\n) \n  ...Orders_data_2g7x2d\n\n\nfragment AccountLink_data on AccountType \n  address\n  chain \n    identifier\n    id\n  \n  user \n    publicUsername\n    id\n  \n  ...ProfileImage_data\n  ...wallet_accountKey\n\n\nfragment AskPrice_data on OrderV2Type \n  dutchAuctionFinalPrice\n  openedAt\n  priceFnEndedAt\n  makerAssetBundle \n    assetQuantities(first: 30) \n      edges \n        node \n          ...quantity_data\n          id\n        \n      \n    \n    id\n  \n  takerAssetBundle \n    assetQuantities(first: 1) \n      edges \n        node \n          ...AssetQuantity_data\n          id\n        \n      \n    \n    id\n  \n\n\nfragment AssetCell_assetBundle on AssetBundleType \n  assetQuantities(first: 2) \n    edges \n      node \n        asset \n          collection \n            name\n            id\n          \n          name\n          ...AssetMedia_asset\n          ...asset_url\n          id\n        \n        relayId\n        id\n      \n    \n  \n  name\n  slug\n\n\nfragment AssetMedia_asset on AssetType \n  animationUrl\n  backgroundColor\n  collection \n    description\n    displayData \n      cardDisplayStyle\n    \n    imageUrl\n    hidden\n    name\n    slug\n    id\n  \n  description\n  name\n  tokenId\n  imageUrl\n\n\nfragment AssetQuantity_data on AssetQuantityType \n  asset \n    ...Price_data\n    id\n  \n  quantity\n\n\nfragment Orders_data_2g7x2d on Query \n  orders(after: $cursor, excludeMaker: $excludeMaker, first: $count, isExpired: $isExpired, isFilled: $isFilled, isValid: $isValid, maker: $maker, makerArchetype: $makerArchetype, makerAssetIsPayment: $makerAssetIsPayment, takerArchetype: $takerArchetype, takerAssetCategories: $takerAssetCategories, takerAssetCollections: $takerAssetCollections, takerAssetIsOwnedBy: $takerAssetIsOwnedBy, takerAssetIsPayment: $takerAssetIsPayment, sortAscending: $sortAscending, sortBy: $sortBy, makerAssetBundle: $makerAssetBundle, takerAssetBundle: $takerAssetBundle) \n    edges \n      node \n        closedAt\n        isFulfillable\n        isValid\n        oldOrder\n        openedAt\n        orderType\n        maker \n          address\n          ...AccountLink_data\n          ...wallet_accountKey\n          id\n        \n        makerAsset: makerAssetBundle \n          assetQuantities(first: 1) \n            edges \n              node \n                asset \n                  assetContract \n                    account \n                      address\n                      chain \n                        identifier\n                        id\n                      \n                      id\n                    \n                    id\n                  \n                  id\n                \n                id\n              \n            \n          \n          id\n        \n        makerAssetBundle \n          assetQuantities(first: 30) \n            edges \n              node \n                ...AssetQuantity_data\n                ...quantity_data\n                id\n              \n            \n          \n          id\n        \n        relayId\n        side\n        taker \n          ...AccountLink_data\n          ...wallet_accountKey\n          id\n          address\n        \n        takerAssetBundle \n          assetQuantities(first: 1) \n            edges \n              node \n                ...AssetQuantity_data\n                ...quantity_data\n                asset \n                  ownedQuantity(identity: )\n                  decimals\n                  symbol\n                  relayId\n                  assetContract \n                    account \n                      address\n                      id\n                    \n                    id\n                  \n                  id\n                \n                quantity\n                id\n              \n            \n          \n          id\n        \n        ...AskPrice_data\n        ...orderLink_data\n        makerAssetBundleDisplay: makerAssetBundle \n          ...AssetCell_assetBundle\n          id\n        \n        takerAssetBundleDisplay: takerAssetBundle \n          ...AssetCell_assetBundle\n          id\n        \n        id\n        __typename\n      \n      cursor\n    \n    pageInfo \n      endCursor\n      hasNextPage\n    \n  \n\n\nfragment Price_data on AssetType \n  decimals\n  imageUrl\n  symbol\n  usdSpotPrice\n  assetContract \n    blockExplorerLink\n    id\n  \n\n\nfragment ProfileImage_data on AccountType \n  imageUrl\n  address\n  chain \n    identifier\n    id\n  \n\n\nfragment asset_url on AssetType \n  assetContract \n    account \n      address\n      chain \n        identifier\n        id\n      \n      id\n    \n    id\n  \n  tokenId\n\n\nfragment orderLink_data on OrderV2Type \n  makerAssetBundle \n    assetQuantities(first: 30) \n      edges \n        node \n          asset \n            externalLink\n            collection \n              externalUrl\n              id\n            \n            id\n          \n          id\n        \n      \n    \n    id\n  \n\n\nfragment quantity_data on AssetQuantityType \n  asset \n    decimals\n    id\n  \n  quantity\n\n\nfragment wallet_accountKey on AccountType \n  address\n  chain \n    identifier\n    id\n  \n\n"""
    variables = "cursor":None,"count":10,"excludeMaker":None,"isExpired":False,"isFilled":None,"isValid":True,"maker":None,"makerArchetype":None,"makerAssetIsPayment":True,"takerArchetype":"assetContractAddress":"0x7c40c393dc0f283f318791d746d894ddd3693572","tokenId":"7722","takerAssetCategories":None,"takerAssetCollections":None,"takerAssetIsOwnedBy":None,"takerAssetIsPayment":None,"sortAscending":None,"sortBy":"MAKER_ASSETS_USD_PRICE","makerAssetBundle":None,"takerAssetBundle":None
    response = requests.post('https://api.opensea.io/graphql/', json='query': query,data=variables)
    print(response.text)

(在变量中,“assetContractAddress”和“tokenId”对于项目是唯一的。

但是当我运行它时,我得到:

"errors":["message":"必须提供查询字符串。"]

如果我不在requests.post 中使用data=variables,我会得到:

"errors":["message":"[400] taker_asset_categories、taker_asset_collections、maker、taker、maker、include_maker_assets、include_taker_assets、maker_assets、taker_assets、maker_asset_is_owned_by、taker_asset_is_owned_by、exclude_maker、maker_asset_bundle、taker_asset_bundle 之一需要已定义。","locations":["line":118,"column":3],"path":["orders"]],"data":"orders":null

如何将requests.post 与查询和变量一起使用以获得正确的响应?

谢谢!

【问题讨论】:

1.您应该向requests.post() 提供datajson,但不能同时提供两者。 2. 阅读您尝试使用的 API 的文档,以确定要传递给它的正确数据。错误消息会提示您缺少什么。 1.谢谢,很高兴知道。 2.api.opensea.io/graphql 的文档根本没有帮助。我有查询及其变量的值,但我看不到如何将它们与查询链接。 【参考方案1】:

您需要像这样发送 json 数据中的变量:


    "query": "your query",
    "variables": 
        "var1": "value1"
    

结帐this

例子:

import requests

query = """query OrdersQuery(\n  $cursor: String\n  $count: Int = 10\n  $excludeMaker: IdentityInputType\n  $isExpired: Boolean\n  $isFilled: Boolean\n  $isValid: Boolean\n  $maker: IdentityInputType\n  $makerArchetype: ArchetypeInputType\n  $makerAssetIsPayment: Boolean\n  $takerArchetype: ArchetypeInputType\n  $takerAssetCategories: [CollectionSlug!]\n  $takerAssetCollections: [CollectionSlug!]\n  $takerAssetIsOwnedBy: IdentityInputType\n  $takerAssetIsPayment: Boolean\n  $sortAscending: Boolean\n  $sortBy: OrderSortOption\n  $makerAssetBundle: BundleSlug\n  $takerAssetBundle: BundleSlug\n) \n  ...Orders_data_2g7x2d\n\n\nfragment AccountLink_data on AccountType \n  address\n  chain \n    identifier\n    id\n  \n  user \n    publicUsername\n    id\n  \n  ...ProfileImage_data\n  ...wallet_accountKey\n\n\nfragment AskPrice_data on OrderV2Type \n  dutchAuctionFinalPrice\n  openedAt\n  priceFnEndedAt\n  makerAssetBundle \n    assetQuantities(first: 30) \n      edges \n        node \n          ...quantity_data\n          id\n        \n      \n    \n    id\n  \n  takerAssetBundle \n    assetQuantities(first: 1) \n      edges \n        node \n          ...AssetQuantity_data\n          id\n        \n      \n    \n    id\n  \n\n\nfragment AssetCell_assetBundle on AssetBundleType \n  assetQuantities(first: 2) \n    edges \n      node \n        asset \n          collection \n            name\n            id\n          \n          name\n          ...AssetMedia_asset\n          ...asset_url\n          id\n        \n        relayId\n        id\n      \n    \n  \n  name\n  slug\n\n\nfragment AssetMedia_asset on AssetType \n  animationUrl\n  backgroundColor\n  collection \n    description\n    displayData \n      cardDisplayStyle\n    \n    imageUrl\n    hidden\n    name\n    slug\n    id\n  \n  description\n  name\n  tokenId\n  imageUrl\n\n\nfragment AssetQuantity_data on AssetQuantityType \n  asset \n    ...Price_data\n    id\n  \n  quantity\n\n\nfragment Orders_data_2g7x2d on Query \n  orders(after: $cursor, excludeMaker: $excludeMaker, first: $count, isExpired: $isExpired, isFilled: $isFilled, isValid: $isValid, maker: $maker, makerArchetype: $makerArchetype, makerAssetIsPayment: $makerAssetIsPayment, takerArchetype: $takerArchetype, takerAssetCategories: $takerAssetCategories, takerAssetCollections: $takerAssetCollections, takerAssetIsOwnedBy: $takerAssetIsOwnedBy, takerAssetIsPayment: $takerAssetIsPayment, sortAscending: $sortAscending, sortBy: $sortBy, makerAssetBundle: $makerAssetBundle, takerAssetBundle: $takerAssetBundle) \n    edges \n      node \n        closedAt\n        isFulfillable\n        isValid\n        oldOrder\n        openedAt\n        orderType\n        maker \n          address\n          ...AccountLink_data\n          ...wallet_accountKey\n          id\n        \n        makerAsset: makerAssetBundle \n          assetQuantities(first: 1) \n            edges \n              node \n                asset \n                  assetContract \n                    account \n                      address\n                      chain \n                        identifier\n                        id\n                      \n                      id\n                    \n                    id\n                  \n                  id\n                \n                id\n              \n            \n          \n          id\n        \n        makerAssetBundle \n          assetQuantities(first: 30) \n            edges \n              node \n                ...AssetQuantity_data\n                ...quantity_data\n                id\n              \n            \n          \n          id\n        \n        relayId\n        side\n        taker \n          ...AccountLink_data\n          ...wallet_accountKey\n          id\n          address\n        \n        takerAssetBundle \n          assetQuantities(first: 1) \n            edges \n              node \n                ...AssetQuantity_data\n                ...quantity_data\n                asset \n                  ownedQuantity(identity: )\n                  decimals\n                  symbol\n                  relayId\n                  assetContract \n                    account \n                      address\n                      id\n                    \n                    id\n                  \n                  id\n                \n                quantity\n                id\n              \n            \n          \n          id\n        \n        ...AskPrice_data\n        ...orderLink_data\n        makerAssetBundleDisplay: makerAssetBundle \n          ...AssetCell_assetBundle\n          id\n        \n        takerAssetBundleDisplay: takerAssetBundle \n          ...AssetCell_assetBundle\n          id\n        \n        id\n        __typename\n      \n      cursor\n    \n    pageInfo \n      endCursor\n      hasNextPage\n    \n  \n\n\nfragment Price_data on AssetType \n  decimals\n  imageUrl\n  symbol\n  usdSpotPrice\n  assetContract \n    blockExplorerLink\n    id\n  \n\n\nfragment ProfileImage_data on AccountType \n  imageUrl\n  address\n  chain \n    identifier\n    id\n  \n\n\nfragment asset_url on AssetType \n  assetContract \n    account \n      address\n      chain \n        identifier\n        id\n      \n      id\n    \n    id\n  \n  tokenId\n\n\nfragment orderLink_data on OrderV2Type \n  makerAssetBundle \n    assetQuantities(first: 30) \n      edges \n        node \n          asset \n            externalLink\n            collection \n              externalUrl\n              id\n            \n            id\n          \n          id\n        \n      \n    \n    id\n  \n\n\nfragment quantity_data on AssetQuantityType \n  asset \n    decimals\n    id\n  \n  quantity\n\n\nfragment wallet_accountKey on AccountType \n  address\n  chain \n    identifier\n    id\n  \n\n"""
variables = "cursor": None, "count": 10, "excludeMaker": None, "isExpired": False, "isFilled": None, "isValid": True, "maker": None, "makerArchetype": None, "makerAssetIsPayment": True, "takerArchetype": "assetContractAddress": "0x7c40c393dc0f283f318791d746d894ddd3693572",
                                                                                                                                                                                                              "tokenId": "7722", "takerAssetCategories": None, "takerAssetCollections": None, "takerAssetIsOwnedBy": None, "takerAssetIsPayment": None, "sortAscending": None, "sortBy": "MAKER_ASSETS_USD_PRICE", "makerAssetBundle": None, "takerAssetBundle": None
response = requests.post('https://api.opensea.io/graphql/',
    json="query": query, "variables": variables
)
print(response.text)

【讨论】:

似乎不起作用。看起来 Cloudflare 已被设置为保护层。

以上是关于Python 请求使用变量向 graphql 发布查询的主要内容,如果未能解决你的问题,请参考以下文章

Python GraphQL 变量未定义

使用变量时的 GraphQL 错误请求

使用 Python 请求发布 GraphQL 突变

如何使用 Firebase Auth 作为 OIDC 向 Amplify GraphQL 发出经过身份验证的请求?

使用 GraphQL Body 在 python3 中发布请求

GraphQL java:当请求有未使用的变量时抛出异常