贝宝 UNSUPPORTED_MEDIA_TYPE

Asu*_*sur 7 paypal fetch

我正在尝试从贝宝的授权 api 获取访问令牌。当我向 api 发出 post 请求时,我得到UNSUPPORTED_MEDIA_TYPE415 响应。

下面是我使用的片段。

const auth = await fetch(PAYPAL_OAUTH_API, {
    method: 'post',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Basic ${ basicAuth }`
    },
    body: JSON.stringify({"grant_type": "client_credentials"})
});
Run Code Online (Sandbox Code Playgroud)

Asu*_*sur 7

我已通过设置Content-Typeapplication/x-www-form-urlencoded.

我的猜测是贝宝只接受application/x-www-form-urlencoded授权 api。


Seb*_*čić 7

我遇到了同样的问题,解决方案如下(使用 Postman):

  1. 选择 POST
  2. 添加TokenAuthorization,类型为 Bearer-Token
  3. Content-Type: application/json在标题中选择
  4. 使用RAW如的身体,并在TEXT下拉列表中,选择JSON( application/JSON
  5. 将 body 复制为原始对象并相应地更改信息。

第 4 步和第 5 步是解决错误的方法,您必须发送原始 json 对象。