Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request header field authorization is not allowed by Access-Control-Allow-Headers AND Accept-Encoding error #95

Closed
muhammed671 opened this issue Nov 8, 2021 · 11 comments

Comments

@muhammed671
Copy link

muhammed671 commented Nov 8, 2021

Hi,

im triyng to connect to the AOI but I get alwasy this issue:

Access to XMLHttpRequest at 'https://api.sandbox.ebay.com/identity/v1/oauth2/token' from origin 'http://localhost:8100' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

Here is my code:

const eBayApi = require('@hendt/ebay-api')

const eBay = new eBayApi({
  appId: 'xxxxxxxxx',
  certId: 'xxxxxxxxx',
  devId: 'xxxxxxxx',
  sandbox: true,
  siteId: eBayApi.SiteId.EBAY_DE,
  marketplaceId:  eBayApi.MarketplaceId.EBAY_DE,
  acceptLanguage: eBayApi.Locale.en_DE, 
  contentLanguage: eBayApi.ContentLanguage.en_DE, 
});

const item = await eBay.buy.browse.getItem('v1|254188828753|0');
console.log(JSON.stringify(item, null, 2));

image

How I can solve this problem?

@dantio
Copy link
Collaborator

dantio commented Nov 9, 2021

You have to use a proxy since you are using it from the browser. Take a look in browser examples and here https://hendt.github.io/ebay-api/

@muhammed671
Copy link
Author

Thanks for the feedback

It's solved. I put this config (only for testing):

eBay.req.instance.interceptors.request.use((request) => {
  request.url = 'https://ebay.hendt.workers.dev/' + request.url;
  return request;
});

But now I have Accept-Encoding error:

image

This is my header:

  private httpOptions = {
    headers: new HttpHeaders({ 
      'Authorization': 'Basic ' + btoa(appId + ':' + certId),
      'Accept': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept-Encoding': 'gzip',
      'Content-Encoding': 'gzip'
    })
  };

How I can solve this problem?

@dantio
Copy link
Collaborator

dantio commented Nov 9, 2021

Why do set the headers? It's not required. Just take a look in the source code of the page I posted.

@muhammed671
Copy link
Author

That's my full configuration. But same issiue with "Accept-Encoding"

import  eBayApi  from '@hendt/ebay-api';

const eBay = new eBayApi({
  appId: appId,
  certId: certId,
  sandbox: false,
});

eBay.req.instance.interceptors.request.use((request) => {
  // Add Proxy
  request.url = 'https://ebay.hendt.workers.dev/' + request.url;
  return request;
});

eBay.commerce.taxonomy.getCategoryTree('77').then(data=>console.log(data))

@dantio
Copy link
Collaborator

dantio commented Nov 10, 2021

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@hendt/ebay-api@latest/lib/ebay-api.min.js"></script>
</head>
<body>
<script>
  
const eBay = new eBayApi({
  appId: '-',
  certId: '-'',
  sandbox: false,
});
  
eBay.req.instance.interceptors.request.use((request) => {
  request.url = 'https://ebay.hendt.workers.dev/' + request.url;
  return request;
});
  
  eBay.commerce.taxonomy.getCategoryTree('77').then(data=>console.log(data))
  </script>
</body>
</html>

Tested with Chrome and FF. Both works for me.

@dantio
Copy link
Collaborator

dantio commented Nov 11, 2021

I'm able to reproduce the error now. I'm on it.

@dantio
Copy link
Collaborator

dantio commented Nov 11, 2021

@muhammed671 v5.0.3 should fix this issue.

@muhammed671
Copy link
Author

Thanks for fixing. In my local test envairment it looks good. Next step, needs to check it in production on the server with my own proxy.

I will let you know about the result.

@muhammed671
Copy link
Author

It works also on production with own proxy.

Used Proxy Server:
https://github.com/Rob--W/cors-anywhere

@dantio Thanks for support and fixing the issue

@muhammed671 muhammed671 changed the title Request header field authorization is not allowed by Access-Control-Allow-Headers Request header field authorization is not allowed by Access-Control-Allow-Headers AND Accept-Encoding error Nov 11, 2021
@dantio
Copy link
Collaborator

dantio commented Nov 11, 2021

Nice! Thank you, I'll add cors-anywhere to docs also.

@dantio dantio closed this as completed Nov 11, 2021
@muhammed671
Copy link
Author

muhammed671 commented Nov 11, 2021

Nice! Thank you, I'll add cors-anywhere to docs also.

Only for your Docs. The Proxy from Rob--W/cors-anywhere works very well with heroku.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants