【问题标题】:How to call the API of AppSync in curl from my local machine?如何从本地机器调用 curl 中的 AppSync 的 API?
【发布时间】:2023-04-05 21:20:01
【问题描述】:
我使用 AppSync 创建了一个 API。现在我想用 curl 调用它,我得到以下错误:You are not authorized to make this call.
我猜到了以下几点:
curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer da2-XXXXXXXXXXXXXXXXXXXXXXXXXX" -d '{"query":"listMyModelTypes{listMyModelTypes {items {id title}}}"}' https://wuw4mcnvautpl4v5ox33fdzoq.appsync-api.us-east-1.amazonaws.com/graphql
或者我还应该在查询中的某处包含 API ID?
【问题讨论】:
标签:
amazon-web-services
aws-appsync
【解决方案1】:
通过 CURL 或 Postman 进行 Appsync 查询取决于获取正确的请求正文和标头。所需的标头取决于身份验证类型。
# common variables
API_URL='https://<APPSYNC-ID>.appsync-api.eu-west-1.amazonaws.com/graphql'
QUERY='query GetImages($t: String!) { images(topic:$t) { edges { cursor } } }'
VARIABLES='{"t":"cats"}' # no spaces!
API 密钥验证:x-api-key 标头
API_KEY='da2-XXXXXXXXXXXXXXXXXXXXXXXXXX'
curl -s -XPOST -H "Content-Type:application/graphql" -H "x-api-key:$API_KEY" -d '{"query": "'"$QUERY"'", "variables": '$VARIABLES'}' $API_URL
基于令牌的身份验证(例如 Cognito):Authorization 和 host 标头
TOKEN='<YOUR JWT AUTH TOKEN HERE>'
HOST='<APPSYNC-ID>.appsync-api.eu-west-1.amazonaws.com'
curl -s -XPOST -H "Content-Type:application/graphql" -H "Authorization:$TOKEN" -H "host:$HOST" -d '{"query": "'"$QUERY"'", "variables": '$VARIABLES'}' $API_URL