【问题标题】:Getting access token from the PayPal API Error从 PayPal API 错误获取访问令牌
【发布时间】:2020-09-04 07:53:41
【问题描述】:

//秘密在上面定义(javascript)

PAYPAL_OAUTH_API = "https://api.sandbox.paypal.com/v1/oauth2/token/";

// 1c. Get an access token from the PayPal API
basicAuth = `${PAYPAL_CLIENT}:${PAYPAL_SECRET}`;
const token = await axios.post(
  PAYPAL_OAUTH_API,
  `grant_type=client_credentials`,
  {
    headers: {
      Accept: `application/json`,
      Authorization: `Basic ${basicAuth}`,
    },
  }
);
return token;

它给了我错误“请求失败,状态码 401 我认为问题出在grant_type=client_credentials 语法中。这是将参数传递给 Axios 以进行发布请求的正确语法吗?

【问题讨论】:

  • 根据documentation,这些是API的正确参数。
  • 最好使用{grant_type: 'client_credentials'},Axios 会将其转换为 URL 编码的字符串。但是你写的应该行得通。
  • 401 错误意味着您没有提供正确的凭据。您确定您使用的是沙盒密钥而不是生产密钥吗?
  • @Barmar 它对我有用。谢谢。但现在它给出了 UNSUPPORTED_MEDIA_TYPE 错误。

标签: javascript paypal


【解决方案1】:

${basicAuth}必须是base64编码,试试${btoa(basicAuth)}

curl 的 -u 标志会为您执行此操作

【讨论】:

  • ... 使用 axios 的 auth 选项也是如此 :-)
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 2023-04-05
  • 2013-11-28
  • 1970-01-01
  • 2021-01-10
  • 2018-09-26
  • 2018-07-24
  • 2018-10-10
相关资源
最近更新 更多