首页 > 解决方案 > 使用 PowerShell 查询 GraphQL

问题描述

所以我一直在尝试从 PowerShell 查询 GraphQL。一切在 cmd 中看起来都很完美,但我无法让它在 PS 中工作,看,到目前为止我有这个代码:

$oAuth2TokenUrl = "https://api.cloudflare.com/client/v4/zones/0000/analytics/dashboard"
$accessKey = '1111' 

$Cloudflare_Oauth_Header = @{
    "Authorization" = "Bearer $accessKey";
}

$query = @'
{ "query": "query { viewer {
zones(filter: {zoneTag: 0000}) {
  httpRequests1mGroups(orderBy: [datetimeMinute_ASC], limit: 1000, filter: {datetime_geq: "2019-09-08T20:00:00Z", datetime_lt: "2019-09-08T20:02:00Z"}) {
    dimensions {datetimeMinute}
    sum {
      browserMap {
        pageViews
        uaBrowserFamily
      }
      bytes
      cachedBytes
      cachedRequests
      contentTypeMap {
        bytes
        requests
        edgeResponseContentTypeName
      }
      clientSSLMap {
        requests
        clientSSLProtocol
      }
      countryMap {
        bytes
        requests
        threats
        clientCountryName
      }
      encryptedBytes
      encryptedRequests
      ipClassMap {
        requests
        ipType
      }
      pageViews
      requests
      responseStatusMap {
        requests
        edgeResponseStatus
      }
      threats
      threatPathingMap {
        requests
        threatPathingName
      }
    }
    uniq {
      uniques
    }
  }
}
  } }" }
'@


$Cloudflare_zone = Invoke-RestMethod -Method Post -Headers $Cloudflare_Oauth_Header -ContentType "application/json; charset=utf-8" -Uri $oAuth2TokenUrl -Body $query 

而且我不断收到错误Invoke-RestMethod : The remote server returned an error: (501) Not Implemented. 但是如果我在 cmd 中卷曲它会返回预期的内容并且没有显示错误。

如果您需要更多详细信息,请告诉我。

标签: powershellcurlgraphqlcloudflare

解决方案


我建议先让一个简单的查询起作用,然后将其扩展为更复杂。此外,请务必使用-ContentType "application/json"某些 GraphQL 服务器需要的。该开关-UseBasicParsing还可以帮助避免错误,但可能不是这里的问题。最后,您可能还需要在 URL 中使用斜杠:例如http://127.0.0.1:8080/graphql/

这个线程也可能有帮助: https ://www.reddit.com/r/PowerShell/comments/b9jasa/query_graphql_with_powershell/


推荐阅读