连接到 Amazon SP-API 时出现 Cors 预检错误

问题描述

我正在尝试连接到亚马逊的销售合作伙伴 API 以请求和检索报告。按照他们的指导,我创建并配置了 IAM 策略,注册并自行授权了我的应用程序,并且能够使用我的客户端信息和刷新令牌检索访问代码

然后我成功地使用 CryptoJS 散列了我的请求,并能够生成 aws4 签名。

但是,当我使用 fetch() 向端点 https://sellingpartnerapi-eu.amazon.com/reports/2020-09-04/reports 发送“发布”请求时,我收到 cors 错误

对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式为 'no-cors' 以在禁用 CORS 的情况下获取资源。

如果我在请求中包含 'mode': 'no-cors',则不会在标头中发送 'x-amz-access-token'。

我无法弄清楚为什么我的 fetch() 请求可以用于检索访问代码

const getAuth = () => {
    fetch('https://api.amazon.com/auth/o2/token',{
        method: 'post',headers: {
            'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },body: `grant_type=refresh_token&refresh_token=${refreshToken}&client_id=${clientId}&client_secret=${clientSecret}`
    })
    .then...

...但不创建报告。

const createReport = (accesstoken,signature) => {
    fetch('https://sellingpartnerapi-eu.amazon.com/reports/2020-09-04/reports?reportType=GET_MERCHANT_LISTINGS_DATA_LITER&MarketplaceId=A1F83G8C2ARO7P',headers: {
            'Authorization': `AWS4-HMAC-SHA256 Credential=${accessKey}/${credentialScope},SignedHeaders=host;x-amz-access-token;x-amz-date;user-agent,Signature=${signature}`,'host': 'sellingpartnerapi-eu.amazon.com','x-amz-access-token': accesstoken,'x-amz-date': amzDate,'user-agent': 'Stock Levels/1.0 (Language=Javascript;)'
        }
    }).then...

此外,SP-API 文档要求在请求中发送“主机”和“用户代理”标头,但我的理解是两者都不允许。

这是 JavaScript 中的限制吗,我是否应该寻找替代语言来发送这些请求?

对这位新手开发者的任何帮助将不胜感激!

谢谢

[连接到销售合作伙伴 API]https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/developer-guide/SellingPartnerApiDeveloperGuide.md#step-3-add-headers-to-the-uri

[请求报告]https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/reports-api-use-case-guide/reports-api-use-case-guide-2020-09-04.md#step-1-request-a-report

[createReport]https://github.com/amzn/selling-partner-api-docs/blob/main/references/reports-api/reports_2020-09-04.md#createreport

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)