AWS AppSync gives Unauthorized Access

0
session = requests.Session()
session.auth = AWS4Auth(
    # An AWS 'ACCESS KEY' associated with an IAM user.
    AWS_ACCESS_KEY_ID,
    # The 'secret' that goes with the above access key.                    
    AWS_SECRET,    
    # The region you want to access.
    AWS_REGION_NAME,
    # The service you want to access.
    'appsync'
)
# As found in AWS Appsync under Settings for your endpoint.
APPSYNC_API_ENDPOINT_URL = APPSYNC_ENDPOINT
# Use JSON format string for the query. It does not need reformatting.
query = """query MyQuery {
          getCustomer(id: "38c76f4f-c495-42e6-8599-647738a12692") {
            first_name
          }
        }"""
# Now we can simply post the request...
response = session.request(
    url=APPSYNC_API_ENDPOINT_URL,
    method='POST',
    json={'query': query}
)
print(response.text)

I want to be able to Read/Write data from AWS AppSync's dynamodb through python. I came across this solution online. But when I run it gives me the following error:

{
  "errors" : [ {
    "errorType" : "UnauthorizedException",
    "message" : "You are not authorized to make this call."
  } ]
}

I have given Administrator Access to the IAM User. What am I doing wrong here?

1개 답변
0

This error implies that you have a different authorization method enabled on AppSync than "AWS Identity and Access Management (IAM)". The default authorization method is "API key".

Changing the authorization method:

  1. Navigate to the AppSync resource in the AWS Console
  2. Click on the Settings option in the left-hand menu
  3. Select default authorization mode as shown below Enter image description here

Using API Key Sample

To change your python code to use API key, please refer to the below sample

import requests

session = requests.Session()

session.headers = {'x-api-key': '<INSERTAPIKEYHERE'} 
# As found in AWS Appsync under Settings for your endpoint.
APPSYNC_API_ENDPOINT_URL = APPSYNC_ENDPOINT
# Use JSON format string for the query. It does not need reformatting.
query = """query MyQuery {
          getCustomer(id: "38c76f4f-c495-42e6-8599-647738a12692") {
            first_name
          }
        }"""
# Now we can simply post the request...
response = session.request(
    url=APPSYNC_API_ENDPOINT_URL,
    method='POST',
    json={'query': query}
    
)
print(response.text)
profile pictureAWS
답변함 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인