【问题标题】:How do I refresh a Smartcar access token?如何刷新 Smartcar 访问令牌?
【发布时间】:2019-11-30 23:38:21
【问题描述】:

我在我的 Tesla (https://teslaapi.dev/) 上使用 Smartcar API,之前成功发出过请求,但我认为访问令牌已过期,我不知道如何刷新它。

我遵循了这个指南:https://smartcar.com/docs/integration-guides/express/request/ 它讨论了访问令牌,但没有告诉我如何获取刷新令牌。

// ./index.js

app.get('/vehicle', function(req, res) {
    // TODO: Request Step 2: Get vehicle information
    return smartcar.getVehicleIds(access.accessToken)
      .then(function(data) {
        // the list of vehicle ids
        return data.vehicles;
      })
      .then(function(vehicleIds) {
        // instantiate the first vehicle in the vehicle id list
        const vehicle = new smartcar.Vehicle(vehicleIds[0], access.accessToken);

        return vehicle.info();
      })
    .then(function(info) {
      res.render('vehicle', {
        info: info,
      });
    });
});

这不再起作用了: { "error": "authentication_error", "message": "提供的令牌无效或过期。" }

我认为这是因为我需要将 accessToken 替换为刷新令牌。我该怎么做?

【问题讨论】:

    标签: node.js api oauth oauth-2.0 automotive


    【解决方案1】:

    所以您对需要使用刷新令牌的预感是正确的。

    如果您查看 API 参考的 "Request access token" 部分,它会注意到访问令牌仅在 2 小时内有效,之后您将需要使用刷新令牌来获取要使用的新访问令牌。

    如果您使用的是 Node SDK,则可以使用 exchangeRefreshToken 方法将您的刷新令牌交换为一组新令牌。

    这是一个集成了所有这些的示例:

    // ./index.js
    
    app.get('/vehicle', function(req, res) {
        if (smartcar.isExpired(acccess.expiration)) {
          const 
       }
    
        // TODO: Request Step 2: Get vehicle information
        return smartcar.getVehicleIds(access.accessToken)
          .then(function(data) {
            // the list of vehicle ids
            return data.vehicles;
          })
          .then(function(vehicleIds) {
            // instantiate the first vehicle in the vehicle id list
            const vehicle = new smartcar.Vehicle(vehicleIds[0], access.accessToken);
    
            return vehicle.info();
          })
        .then(function(info) {
          res.render('vehicle', {
            info: info,
          });
        });
    });
    

    为了适当地长期实现这一点,您需要使用某种数据库来存储基于车辆 ID 的访问对象。

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2018-09-25
      • 2020-11-09
      • 2014-09-13
      • 2019-06-29
      • 2018-01-25
      • 1970-01-01
      • 2016-08-13
      • 2016-09-23
      相关资源
      最近更新 更多