Error 401 when trying to call appsync from s3 bucket using aws-amplify
See original GitHub issueWhat AWS Services are you utilizing? aws-amplify,aws-cognity,aws-appsync,amazon s3
Provide additional details e.g. code snippets
After setting up appsync with cognito user pool, connecting to it using aws amplify react.js library
from local host, works fine but after uploading it to an s3 bucket, I just receive 401 error :
x-amzn-errortype: UnauthorizedException
x-amzn-requestid: eb7e23c8-e02f-4906-831c-2528accc46ae
x-cache: Error from cloudfront
I checked network tab of chrome. in request header there was jwt token .
The wierd thing is when I tried other hostings, they work fine too. so it seems a problem in s3 configuration or maybe amplify’s appsync API does not work on static hosting solution. Does anyone know what can cause this problem?
My s3 bucket permissions :
{
"Version": "2012-10-17",
"Id": "HostingPolicy",
"Statement": [
{
"Sid": "PublicAccess",
"Effect": "Allow",
"Principal": "*",
"Action": "*",
"Resource": "arn:aws:s3:::test-hosting/*"
}
]
}
S3 Cors configuration :
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<ID>test-users-bucket-cors-rule</ID>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
<ExposeHeader>ETag</ExposeHeader>
<ExposeHeader>authorization</ExposeHeader>
<ExposeHeader>Authorization</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Simplified react.js
code :
const awsmobile ={
"aws_appsync_region": "...",
"aws_appsync_graphqlEndpoint": "https://....appsync-api.ap-southeast-2.amazonaws.com/graphql",
"aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
Auth: {
identityPoolId: "...",
region: "...",
userPoolId: "...",
userPoolWebClientId: "...",
}
};
Amplify.configure(awsconfig);
//works fine in localhost but not on s3 bucket !
const users = await API.graphql(graphqlOperation(listUsers));
console.log(JSON.stringify(users.data))
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Error 401 when trying to call appsync from s3 bucket using ...
After setting up appsync with cognito user pool, connecting to it using aws amplify react.js library from local host, works fine but after ......
Read more >Resolve unauth errors for GraphQL requests in AWS AppSync
401 Unauthorized: The request is denied by either AWS AppSync or the authorization mode because the credentials are missing or invalid. 200 OK ......
Read more >AWS-Amplify/Lobby - Gitter
Currently I have my S3 bucket set up with a "Public" folder. ... doesn't have the Authorization header on it and so I...
Read more >Aws Appsync Error 401 With Userpool In React, But ... - ADocLib
@yuth It's because I was using the AppSync SDK. It wasn't clear to me that I'm new to AWS and am trying to...
Read more >Enabling unauthenticated access with AWS Amplify
AWS AppSync API service, based on GraphQL API, requires authorization for applications to interact with it. The preferred method of ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try adding the following to your
awsconfig
:I created custom config for aws amplify, which includes below:
Amplify.configure({ //app sync configurations
}
I even tried to add below, but I am still getting missing authorization header graphql_headers: async () => { const currentSession = await Auth.currentSession(); return { Authorization: currentSession.getIdToken().getJwtToken() }; },
Please advise