Amazon cognito 如何修复来自Apollo客户端的格式错误的身份验证标头错误

Amazon cognito 如何修复来自Apollo客户端的格式错误的身份验证标头错误,amazon-cognito,apollo,react-apollo,apollo-client,hasura,Amazon Cognito,Apollo,React Apollo,Apollo Client,Hasura,我正在尝试使用Apollo客户端将react应用程序连接到hasura后端api,但收到以下错误: 错误:GraphQL错误:授权标头格式错误 我不知道是什么引起的 我知道我从Cognito那里得到了一个有效的id令牌,因为我可以将它粘贴到Hasura控制台中,并且工作正常 我只是将apollo授权示例粘贴到我的index.js文件中,并放入我的uri。我开始使用apollo boost,但由于同样的错误,我去了apollo客户端。显然,这没用。我在互联网上搜索过,找不到任何与此相关的东西,这可

我正在尝试使用Apollo客户端将react应用程序连接到hasura后端api,但收到以下错误:

错误:GraphQL错误:授权标头格式错误

我不知道是什么引起的

我知道我从Cognito那里得到了一个有效的id令牌,因为我可以将它粘贴到Hasura控制台中,并且工作正常

我只是将apollo授权示例粘贴到我的index.js文件中,并放入我的uri。我开始使用apollo boost,但由于同样的错误,我去了apollo客户端。显然,这没用。我在互联网上搜索过,找不到任何与此相关的东西,这可能意味着我在做一些愚蠢的事情

以下是我的index.js中的apollo客户端代码:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import ApolloClient from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloProvider } from 'react-apollo';
import { Auth } from 'aws-amplify';

async function getSession() {
    Auth.currentSession().then(res=>{
        let idToken = res.getIdToken()
        let jwt = idToken.getJwtToken()
        //if I console log jwt here I can paste it into hasura and it works
        return jwt
      })
  }

const token = getSession()

//copied from apollo docs:
const httpLink = createHttpLink({
    uri: 'https://api.example.com/v1/graphql',
  });

const authLink = setContext((_, { headers }) => {
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : "",
      }
    }
  });

const client = new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache()
  });
以下是错误:

Error: GraphQL error: Malformed Authorization header
at new ApolloError (bundle.esm.js:76)
at ObservableQuery.getCurrentResult (bundle.esm.js:200)
at ObservableQuery.currentResult (bundle.esm.js:163)
at Query._this.getQueryResult (react-apollo.esm.js:247)
at finish (react-apollo.esm.js:434)
at Query.render (react-apollo.esm.js:441)
at finishClassComponent (react-dom.development.js:15319)
at updateClassComponent (react-dom.development.js:15274)
at beginWork (react-dom.development.js:16262)
at performUnitOfWork (react-dom.development.js:20279)
at workLoop (react-dom.development.js:20320)
at renderRoot (react-dom.development.js:20400)
at performWorkOnRoot (react-dom.development.js:21357)
at performWork (react-dom.development.js:21267)
at performSyncWork (react-dom.development.js:21241)
at requestWork (react-dom.development.js:21096)
at scheduleWork (react-dom.development.js:20909)
at Object.enqueueForceUpdate (react-dom.development.js:11632)
at Query.push../node_modules/react/cjs/react.development.js.Component.forceUpdate (react.development.js:355)
at Query._this.updateCurrentData (react-apollo.esm.js:214)
at Object.error (react-apollo.esm.js:197)
at notifySubscription (Observable.js:157)
at onNotify (Observable.js:196)
at SubscriptionObserver.error (Observable.js:253)
at bundle.esm.js:541
at Array.forEach (<anonymous>)
at iterateObserversSafely (bundle.esm.js:540)
at Object.onError [as error] (bundle.esm.js:482)
at invoke (bundle.esm.js:1532)
at bundle.esm.js:1565
at bundle.esm.js:1986
at Set.forEach (<anonymous>)
at bundle.esm.js:1984
at Map.forEach (<anonymous>)
at QueryManager.broadcastQueries (bundle.esm.js:1982)
at bundle.esm.js:2109
at Object.next (Observable.js:333)
at notifySubscription (Observable.js:152)
at onNotify (Observable.js:196)
at SubscriptionObserver.next (Observable.js:248)
at bundle.esm.js:1079
at Set.forEach (<anonymous>)
at Object.next (bundle.esm.js:1078)
at notifySubscription (Observable.js:152)
at onNotify (Observable.js:196)
at SubscriptionObserver.next (Observable.js:248)
at notifySubscription (Observable.js:152)
at onNotify (Observable.js:196)
at SubscriptionObserver.next (Observable.js:248)
at bundle.esm.js:107

任何帮助都将不胜感激。

分配给
令牌的值属于
承诺类型,而不是您期望的字符串。

由于
getSession()
是一个异步函数,因此它返回一个承诺

这一行:

const token = getSession()
将承诺而非其解析值分配给
令牌


在将结果分配给
令牌之前,您需要等待承诺得到解决

触发此错误的查询是标准查询还是订阅?另外,您显示的标题是针对飞行前请求的,而不是实际请求,应该是下面的请求。您是否有可能发布后续请求的标题(通常应包含“授权”标题)。此外,您如何处理API端的“授权”标题?
const token = getSession()