首页 > 解决方案 > React Native 中的 AWS Appsync 401 和 403 错误

问题描述

我一直在努力在我的 React Native 应用程序中设置 Appsync 和 Cognito。

我尝试了以下两种方法:

Amplify.configure(config);

OR

Amplify.configure({
  Auth: {
    region: config.aws_cognito_region, // REQUIRED - Amazon Cognito Region
    userPoolId: config.aws_user_pools_id, // OPTIONAL - Amazon Cognito User Pool ID
    userPoolWebClientId: config.aws_user_pools_web_client_id, // User Pool App Client ID
  },
});

const client = new AWSAppSyncClient({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authType,
    jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
  },
});

OR

const client = new AWSAppSyncClient({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authType,
    apiKey: appSyncConfig.apiKey,
  },
});

我还学习了这两个教程Tackling user authBuilding a notes app

在这两种情况下,我都在 GraphQL 中收到以下错误,没有描述:

Error: Network error: Response not successful: Received status code

这是在授权类型为 时Amazon Cognito User Pool。我也试过AWS Identity and Access Management (IAM)了,但这给了我一个 403 错误。有人可以指出我可以进一步调试的方向吗?

标签: amazon-web-servicesreact-nativeamazon-cognitoamazon-iamaws-appsync

解决方案


这可能是您阅读的文档/文章中的拼写错误。尝试更换:

auth: {
type: appSyncConfig.authType,
apiKey: appSyncConfig.apiKey
}

和 :

auth: {
type: appSyncConfig.authenticationType,
apiKey: appSyncConfig.apiKey
}

推荐阅读