【问题标题】:How to pass Authorization Header to GraphQL using Amplify in React Native如何在 React Native 中使用 Amplify 将授权标头传递给 GraphQL
【发布时间】:2019-11-12 19:33:08
【问题描述】:

全部,

我们正在使用 React Native 开发移动应用程序,我们正在使用 APP Sync 进行一些操作。

之前我们使用 API_KEY 进行授权,graphql 代码很简单。我们配置好所有需要的键,直接在

中进行查询

API.graphql( graphqlOperation(更新数据,{ ID, 记录 }) )

现在我们更改了授权类型,由于一些安全更改,从 API 密钥移动到从服务器的 cognito 访问令牌。我们获得了授权的访问令牌,现在我们如何为每个 Graphql 请求发送授权令牌或如何处理它?

谁能解释一下

【问题讨论】:

  • 您是否更改了身份验证类型?:行:"aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS", 文件中:src/aws-exports.js

标签: react-native amazon-cognito aws-amplify


【解决方案1】:

根据README for the amplify-js library,您有两种选择:

  1. 如果合适,您可以使用内置的 Amplify Auth 功能:
// For React Native
import { withAuthenticator } from 'aws-amplify-react-native';

// Will provide customizable UI for your user to authenticate
export default withAuthenticator(App);
  1. 否则,您可以自己添加标题:
import API from '@aws-amplify/api';
let token = '/* Your API Token */';
let apiName = 'MyApiName';
let path = '/path';
let options = {
  headers: {
    Authorization: `bearer ${token}`
  } // OPTIONAL
}

API.get(apiName, path, options).then(response => {
  // Add your code here
});

这两个示例都是直接从该答案顶部链接的自述文件中提取的,因此我建议您查看它以获取更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2015-07-24
    相关资源
    最近更新 更多