Ronny Springer
Ronny Springer

Reputation: 309

Missing access token for authorization on Ebay Browse API

I try to search items from eBay API. Within the server.js file at my Apollo Server 2, I pass the token string by context property while instantiation (s. Doku: Apollo context argument). So every request contains the authentication HTTP header property. As a tryout, for now, I just use the fixed token string. This will be changed later if I work for the client.

server.js

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(`🚀 Server ready at ${url}`))

resolver method

// A map of functions which return data for the schema.
const resolvers = {
    Query: {
       books(root, { keyword = '' }, context) {
          console.log(context.headers)
           fetch(`https://api.ebay.com/buy/browse/v1/item_summary/?q=${keyword}`)
             .then(response => response.json())
             .then(json => console.log(json))

           return []
     }
   }
}

The context.header contains the authorization property:

{ host: 'localhost:4000',
  connection: 'keep-alive',
  'content-length': '108',
  accept: '*/*',
  origin: 'http://localhost:4000',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
  dnt: '1',
  'content-type': 'application/json',
  referer: 'http://localhost:4000/',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'de,en;q=0.9',
  authorization: 'Bearer v^1.1#i^1#f^0#p^1#r^0#I^3#t^H4sIAAAAAAAAAOV...' 
 }

The JSON response contains the error with errorId 1002. It says Access token is missing in the Authorization HTTP request header.:

{ errors:
   [ { errorId: 1002,
      domain: 'OAuth',
      category: 'REQUEST',
      message: 'Missing access token',
      longMessage: 'Access token is missing in the Authorization HTTP request header.' } ] }

Additionally, I use a new browser tab, enter the URL https://api.ebay.com/buy/browse/v1/item_summary/search?q=test and add the same authorization header property (I use the ModHeader chrome extension). I hit enter, the request works and I get the expected JSON.

It is confusing and I don't know what I'm doing wrong while passing the token. Does somebody have an idea?

Upvotes: 0

Views: 3811

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84657

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,
  },
})

Also bear in mind that the fetch call should be returned inside your resolver, otherwise it won't be awaited.

Upvotes: 1

Related Questions