【问题标题】:node.js / axios AUTHENTICATION_FAILURE with PayPal Subscriptions APInode.js / axios AUTHENTICATION_FAILURE 与 PayPal 订阅 API
【发布时间】:2020-08-16 20:24:39
【问题描述】:

我正在使用 Axios 激活 PayPal 订阅,因为 NODE SDK 不支持订阅激活。为此,我创建了这个生成PayPal 访问令牌的方法:

let getAccessToken = async () => {
    return await axios(options).then((response) => {
        return response.data.access_token
    });
}

选项包含以下详细信息:

const options = {
    method: 'post',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Access-Control-Allow-Credentials': true
    },
    data: qs.stringify(data),
    auth: {
        username: PAYPAL_CLIENT_ID,
        password: PAYPAL_CLIENT_SECRET
    },
    url: 'https://api.sandbox.paypal.com/v1/oauth2/token'
}

这工作正常,但我在激活订阅时遇到了一些问题,这是处理此问题的方法:

let activateSubscription = async (accessToken, subscriptionId) => {

    return await axios.post(
        `${baseURL}/v1/billing/subscriptions/${subscriptionId}/activate`,
        {
            headers: {
                "Authorization": `Bearer ${accessToken}`,
                "Content-Type": 'application/json'
            }
        }).then((data) => {
            return true;
        })
        .catch((error) => {
            console.log(error.response.data);
            return false;
        });
}

基本上我通过了生成的accessTokensubscriptionId,但我得到了这样的响应:

{
  name: 'AUTHENTICATION_FAILURE',
  message: 'Authentication failed due to invalid authentication credentials or a missing Authorization header.',
  links: [
    {
      href: 'https://developer.paypal.com/docs/api/overview/#error',
      rel: 'information_link'
    }
  ]
}

我怀疑是生成的令牌不正确,所以我在邮递员中尝试了发送此请求:

{{host}}/v1/billing/subscriptions/I-7D10FGKVNMD0/activate

返回的内容是204,根据文档所说的here,这是可以的。

请求似乎正确,我做错了什么?

【问题讨论】:

  • 您将其发送到什么 baseURL 以及您要返回什么 HTTP 标头
  • 还有你是怎么得到I-7D10FGKVNMD0的呢?如果它来自具有正常创建设置的智能支付按钮,它将已经处于活动状态
  • @PrestonPHX 你可以在这里看到完整的回复:pastebin.com/325HgQwM 还有baseUrl 也是这样的:https://api.sandbox.paypal.com/v1/billing/subscriptions/I-FLXLWEUS2UXD/activate
  • @PrestonPHX 智能支付按钮自动激活订阅,但我在application_context 中设置了user_action: "CONTINUE",,因为我需要在我的服务器端执行一些额外的检查。所以订阅状态是APPROVED。订阅参考 I-7D10FGKVNMD0 由智能支付按钮中可用的 createSubscription 方法返回:developer.paypal.com/docs/subscriptions/integrate/…

标签: paypal axios paypal-sandbox paypal-subscriptions


【解决方案1】:

您设置的标题错误。从粘贴箱:

data: '{"headers":{"Authorization":"Bearer A21AAEj_0lJjny7Hc1aL7l5irIxOqOjyW_pSfT2WC9APAQFXHTYzKL0womW1mZvS6mKWsWMytMc6H6NIMPMnOK7zhzHKHsSAw","Content-Type":"application/json"}}', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json;charset=utf-8', 'User-Agent': 'axios/0.19.2', 'Content-Length': 170 },

因此,PayPal 实际上并没有获取您的 Authorization 标头

【讨论】:

  • 我对@9​​87654322@ 函数做同样的事情,axios 接受headers 对象,并且我传递了相同的标题。我应该在axios.post 函数中更改哪些内容?
  • .post(url, data, options),您的选项将作为数据发送
  • 所以我改为:axios.post( ${baseURL}/v1/billing/subscriptions/${subscriptionId}/activate, {}, { headers: { "Authorization": Bearer ${accessToken}, "Content-Type": 'application/json' } } 但得到了:The requested action could not be performed, semantically incorrect, or failed business validation.
  • 已激活,但激活时我不应该收到错误
  • 没错,你不应该这样做。我想知道它是如何神秘地激活的 8-)
猜你喜欢
  • 2020-10-10
  • 2021-09-26
  • 1970-01-01
  • 2020-12-19
  • 2021-09-04
  • 2022-09-27
  • 2021-05-24
  • 2015-01-28
  • 2023-02-02
相关资源
最近更新 更多