javascript - 无法将 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/
工作流程应该如下:
- 发出请求以使用客户端 ID 和密钥创建链接令牌。
- 使用链接令牌打开链接。在 onSuccess 回调中,将临时 public_token 发送到您应用的服务器。
- 请求将 public_token 交换为永久 access_token,并将 item_id 交换为新项目。
- 存储 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 脚本执行此操作。谢谢您的帮助!
我看过的其他地方:
- 条带集成
- 在 React 中做
- 令牌文档
- GitHub 上的 SDK
解决方案
我实际上正在做一些非常相似的事情!我发现最简单的方法是使用快速入门来生成 access_token 并将其传递到我在应用程序脚本中的请求中。
推荐阅读
- c# - 如何按某个属性对表进行分组,然后计算 LINQ 中另一个表(带有外键)中存在的那些元素?
- awk - 在 awk 中,你可以同时使用一个模式和一个 END 块吗?
- azure - Azure DevOps 管道构建工件中缺少 Gulp 编译的 CSS 文件夹
- twitter-bootstrap-3 - 网格自动对齐到多行
- ios - iOS:为什么我的活动视图控制器(self)在使用downloadTask下载视频时采用单例(sharedInstance)的地址?
- java - POJO 是否应该连接到视图或控制器?
- php - 如何在 laravel 中建立关系
- android - 在数组中查找重复值并将它们打印到android中的列表视图
- mongodb - 不同的正则表达式和文本搜索结果
- python - 如何使用 Keras API 提取“从输入层到隐藏层”和“从隐藏层到输出层”的权重?