首页 > 解决方案 > 无法将 Google Apps 脚本连接到 Plaid API

问题描述

怎么了团队,我正在尝试使用 Plaid 自动填充 Google 表格,其中包含在我的 Plaid 帐户中设置的财务报表。我最近看到一些公司这样做是为了收费(例如https://www.budgetsheet.net/)。几年前我还看到开发人员完成了它(请参阅https://github.com/hirefrank/plaid-txns-google-sheets)。这就是我想要做的,但 Plaid API 从那时起已经更新,以从使用中删除公钥来代替链接令牌:https ://plaid.com/docs/link/link-token-migration-guide/

工作流程应该如下:

  1. 发出请求以使用客户端 ID 和密钥创建链接令牌。
  2. 使用链接令牌打开链接。在 onSuccess 回调中,将临时 public_token 发送到您应用的服务器。
  3. 请求将 public_token 交换为永久 access_token,并将 item_id 交换为新项目。
  4. 存储 access_token 并使用它为您的用户的项目发出产品请求。

我可以通过以下调用创建 link_token(链接令牌位于responseJSON.link_token):

/************************************************************************************
 * 
 * Creating function to get access token
 * 
 ************************************************************************************/

function getLinkToken(CLIENT_ID, SECRET) {
  
  // Specify headers
  var headers = {
    'Content-Type': 'application/json',
  };

  // Build data for link token
  var data = {
    'client_id': CLIENT_ID,
    'secret': SECRET,
    "client_name": "ryanmcslomo",
    "products": ["auth", 'transactions'],
    "language": "en",
    "country_codes": ["US"],
    "user": {
      // "client_user_id": "unique-per-user",
      "client_user_id": "ryanmcslomo",
    },
  };

  // Build params
  var parameters = {
    'headers': headers,
    'payload': JSON.stringify(data),
    'method': 'post',
    'muteHttpExceptions': true,
  };

  // Call link token API with params
  var url = "https://development.plaid.com/link/token/create";
  var response = UrlFetchApp.fetch(url, parameters);
  var responseJSON = JSON.parse(response);

  //Return JSON containing Link Token
  return responseJSON;
}

但在那之后,我不确定如何使用链接令牌初始化链接(更不用说获取公共令牌以交换访问令牌)。当我尝试将链接令牌作为公共令牌传递来进行身份验证时,它(当然)失败了:

  // Build data for access token
  var data2 = {
    'client_id': CLIENT_ID,
    'secret': SECRET,
    "public_token": responseJSON.link_token
  };

  // Build params
  var parameters2 = {
    'headers': headers,
    'payload': JSON.stringify(data2),
    'method': 'post',
    'muteHttpExceptions': true,
  };

  // Call API with params
  var url2 = "https://development.plaid.com/item/public_token/exchange";
  var response2 = UrlFetchApp.fetch(url2, parameters2);
  var response2JSON = JSON.parse(response2);

  // return JSON response with Link Token
  return response2JSON;
}

响应2JSON:

{
display_message: null
documentation_url: "https://plaid.com/docs/?ref=error#invalid-input-errors"
error_code: "INVALID_PUBLIC_TOKEN"
error_message: 'provided token is the wrong type. expected "public", got "link"'
error_type: " INVALID_INPUT"
...
}

有任何想法吗?不确定如何作为 Google Apps 脚本执行此操作。谢谢您的帮助!

我看过的其他地方:

标签: javascriptapigoogle-apps-scriptgoogle-sheetsplaid

解决方案


我实际上正在做一些非常相似的事情!我发现最简单的方法是使用快速入门来生成 access_token 并将其传递到我在应用程序脚本中的请求中。


推荐阅读