Oauth 2.0 缺少易趣浏览API上授权的访问令牌

Oauth 2.0 缺少易趣浏览API上授权的访问令牌,oauth-2.0,apollo,ebay-api,apollo-server,Oauth 2.0,Apollo,Ebay Api,Apollo Server,我尝试从eBay API中搜索项目。在my的server.js文件中,我在实例化s时通过上下文属性传递令牌字符串。Doku:。因此,每个请求都包含身份验证HTTP头属性。作为试用,目前我只使用固定令牌字符串。如果我为客户工作,这将在以后更改 server.js 从'apollo server'导入{apollo server} 从“./schema”导入架构 const server=新服务器{ 模式, 上下文:{req}=>{ const token='Bearer v^1.1i^1i^3f^

我尝试从eBay API中搜索项目。在my的server.js文件中,我在实例化s时通过上下文属性传递令牌字符串。Doku:。因此,每个请求都包含身份验证HTTP头属性。作为试用,目前我只使用固定令牌字符串。如果我为客户工作,这将在以后更改

server.js

从'apollo server'导入{apollo server} 从“./schema”导入架构 const server=新服务器{ 模式, 上下文:{req}=>{ const token='Bearer v^1.1i^1i^3f^0p^1r^0t^H4sIAAA…'//我的令牌 返回{ …请求, 标题:{ …请求标头, //使用oauth令牌充实头部 授权:令牌, }, } }, }
server.listen.then{url}=>console.log`您看到的头是在请求中发送到GraphQL服务器的头。您所做的只是将它们修改为包含授权标头,然后将整个请求对象包含为您的上下文-您没有将任何标头信息传递给实际从易趣获取数据的提取调用。至少,您希望执行以下操作:

还要记住,fetch调用应该在解析程序中返回,否则它将不会等待

import { ApolloServer } from 'apollo-server'
import schema from './schema'

const server = new ApolloServer({
   schema,
   context: ({ req }) => {
        const token = 'Bearer v^1.1#i^1#I^3#f^0#p^1#r^0#t^H4sIAAA...' // my token

        return {
             ...req,
             headers: {        
                 ...req.headers,
                 // enrich the header with oauth token
                 authorization: token,
             },
        }
    },
})

server.listen().then(({ url }) => console.log(`The headers you see are the ones being sent in the request to your GraphQL server. All you've done is modified them to include the Authorization header and then included your entire request object as your context -- you're not passing any header information to the 
fetch
call actually getting the data from eBay. Minimally, you want to do something like this:

fetch(`https://api.ebay.com/buy/browse/v1/item_summary/?q=${keyword}`, {
  headers: {
    Authorization: context.headers.authorization,
  },
})