【问题标题】:Where to store API (Paypal) access tokens on the server side (node.js)在服务器端 (node.js) 存储 API (Paypal) 访问令牌的位置
【发布时间】:2019-06-26 21:45:53
【问题描述】:

我正在使用 PayPal API 在我的网站上设置订阅付款。我知道有 PayPal 节点 SDK,但不幸的是,他们没有相应地更新它,并且在订阅方面已经过时了。

我已经设法获取访问令牌并使用它来进行调用等。但是每次我调用他们的 API 时都获取访问令牌是没有意义的。存储访问令牌本身的最理想方式是什么,令牌的到期时间和创建令牌的时间。将使用过期时间和创建令牌的时间来检查令牌是否已过期,然后再尝试获取新令牌。

我创建了下面的 js 文件,将通过 await paypal.getToken() 使用

async function getToken(){
  return new Promise((resolve, reject) => {
    if(//Token expired) {

      request.post({
        uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
        headers: {
          "Accept": "application/json",
          "Accept-Language": "en_US",
          "content-type": "application/x-www-form-urlencoded"
        },
        auth: {
          'user': clientID,
          'pass': clientSecret,
        },
        form: {
          "grant_type": "client_credentials"
        }
      }, function(error, response, body) {
        if(error)
        reject(new Error(error))
        else {

          //Set global variable or whats best option with the access token that was obtained  - JSON.parse(body).access_token
          //Set global variable or whats best option with the expiry time - SON.parse(body).expires_in
          //Set global vairable or whats best option with the current time in MS - Date.now()
          resolve(JSON.parse(body).access_token);
        }
      });
    }else {
      // resolve(config.paypal_access_token); //Retrieve access_token from global vairable or whats best option
    }
  });

}

module.exports.getToken = getToken;

【问题讨论】:

    标签: node.js token storage


    【解决方案1】:

    我认为将令牌存储在 DB 以及 Redis 等数据存储中的最佳方式。这是我的想法

    1. 将令牌、生成时间、到期时间存储在数据库中。也在缓存中。
    2. 制作一个中间件,为每个请求添加令牌。
    3. 如果 Redis 服务器重新启动,您会丢失缓存,然后从数据库中获取数据。如果过期重新认证。

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2018-08-05
      • 2017-02-07
      • 2015-12-14
      • 1970-01-01
      • 2017-12-28
      相关资源
      最近更新 更多