格子:如何在前端创建公共令牌(离子/角度)并发送到服务器

时间:2017-11-03 22:07:02

标签: angularjs node.js ionic-framework ionic2 plaid

我正在关注this教程,以便从格式API获取交易/帐户详细信息。格子花呢快速启动指导用户ejs,以便使用onSuccess功能发送到服务器。如何使用离子创建此令牌?

格子快速指南还建议我们使用代码打击

var linkHandler = Plaid.create({
  env: 'sandbox',
  clientName: 'Client Name',
  key: '<PUBLIC_KEY>',
  product: ['auth', 'transactions'],
  token: '<GENERATED_PUBLIC_TOKEN>',
  onSuccess: function(public_token, metadata) {
    // You do not need to repeat the /item/public_token/exchange
    // process when a user uses Link in update mode.
    // The Item's access_token has not changed.
  },

并建议使用此代码

// Create a public_token for use with Plaid Link's update mode
client.createPublicToken(ACCESS_TOKEN, (err, result) => {
  // Handle err
  // Use the generated public_token to initialize Plaid Link in update
  // mode for a user's Item so that they can provide updated credentials
  // or MFA information
  const publicToken = result.public_token;
});

为了创建公共令牌并获取访问令牌。我无法使用此功能,因为我收到错误&#39; Plaid和/或客户端未定义

如何使用Ionic前端和节点后端创建此公共令牌?

这里的工作流程是什么?

提前致谢

2 个答案:

答案 0 :(得分:1)

最近创建了一个Angular库:单击herehere

一开始我在成功实施Plaid时遇到了一些问题;这些提示应有帮助:

  1. 如上面第二个链接所示,实现库。确保在第二步中导入了app.module.ts文件。不要使用文档中提供的jQuery。
  2. 使用该库,将在Angular元素<mr-ngx-plaid-link-button>中自动创建一个按钮。除非您使用DOM或其​​他方法将其更改为在ngAfterViewInit()上键入按钮,否则此按钮将充当提交按钮。
  3. 第三步中的所有五个事件都是各种事件的侦听器。仅在您(a)成功提交了银行凭证(在Sandbox上为user_good / pass_good)并且(b)单击Continue按钮以退出“格子链接”之后,onSuccess()回调函数才会返回公共令牌。

答案 1 :(得分:0)

在服务器端,您需要首先初始化Plaid节点客户端库。您还需要进行交换令牌调用,以便从链接public_token更换API access_token。然后,您将保存access_token并使用它来检索交易和帐户数据:

// Initialize the Plaid API client with your API keys (https://dashboard.plaid.com/account/keys)
// Use plaid.environments.production, plaid.environments.development, or plaid.environments.sandbox
const plaid = require('plaid');
const client = new plaid.Client(client_id, secret, public_key, plaid.environments.sandbox);
client.exchangePublicToken(PUBLIC_TOKEN, function(error, tokenResponse) {
  if (error != null) {
    var msg = 'Could not exchange public_token!';
    console.log(msg + '\n' + error);
  }
  const ACCESS_TOKEN = tokenResponse.access_token;
  const ITEM_ID = tokenResponse.item_id;
  console.log('Access Token: ' + ACCESS_TOKEN);
  console.log('Item ID: ' + ITEM_ID);
  // Now retrieve transactions or account information with the access_token
});

对于Ionic应用中的客户端,在调用link-initialize.js之前,您需要包含Plaid.create脚本。具体做法是:

<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js">
</script>

这是一个完整的客户端HTML示例:

<button id="link-button">Link Account</button>

<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
var handler = Plaid.create({
  clientName: 'Plaid Walkthrough Demo',
  env: 'sandbox',
  key: '[PUBLIC_KEY]', // public_key is at https://dashboard.plaid.com/account/keys
  product: ['auth'],
  onLoad: function() {
    // Optional, called when Link loads
  },
  onSuccess: function(public_token, metadata) {
    // Send the public_token to your app server.
    // The metadata object contains info about the institution the
    // user selected and the account ID, if `selectAccount` is enabled.
    $.post('/get_access_token', {
      public_token: public_token,
    });
  },
  onExit: function(err, metadata) {
    // The user exited the Link flow.      
  }
});
</script>