【发布时间】:2020-12-18 13:21:47
【问题描述】:
开发新手,实际上只是将格子链接集成 (https://plaid.com/docs/#integrating-with-link) 复制并粘贴到客户端代码的基本 HTML 页面中,并直接粘贴到服务器端代码的 back4app 的云代码 (Node JS) 中,然后调用从云端运行,但没有任何反应,我收到以下错误:
- Fetch API 无法加载 file:///create_link_token。对于 CORS 请求,URL 方案必须是“http”或“https”。
- index2.html:42 Uncaught (in promise) TypeError: 无法获取 在 fetchLinkToken (index2.html:42)
- parse.min.js:13 POST https://parseapi.back4app.com/functions/plaidAPI400
- parse.min.js:13 Uncaught (in promise) 错误:无效函数:“plaidAPI” 在句柄错误
我的 Html 代码如下:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/parse/2.1.0/parse.js"></script>
<script type="text/javascript" src="https://npmcdn.com/parse/dist/parse.min.js"></script>
<title>JS Bin</title>
<style>
.inputs{
margin: 30px 0px;
}
.inputs input{
margin: 5px 0px;
height: 20px;
}
</style>
</head>
<body>
<div align="center">
<div class="title">
<h1>Test</h1>
</div>
</div>
<button id="link-button">Link Account</button>
<script src="js/credentials.js"></script>
<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script src="js/server.js"></script>
<script type="text/javascript">
(async function($) {
const fetchLinkToken = async () => {
const response = await fetch('/create_link_token', { method: 'POST' });
const responseJSON = await response.json();
return responseJSON.link_token;
};
const configs = {
token: await fetchLinkToken(),
onLoad: function() {
// Optional, called when Link loads
},
onSuccess: async function(public_token, metadata) {
await fetch('/get_access_token', {
method: 'POST',
body: JSON.stringify({ public_token: public_token }),
});
},
onExit: async function(err, metadata) {
// The user exited the Link flow.
if (err != null) {
// The user encountered a Plaid API error prior to exiting.
if (err.error_code === 'INVALID_LINK_TOKEN') {
handler.destroy();
handler = Plaid.create({
...configs,
token: await fetchLinkToken(),
});
}
}
},
onEvent: function(eventName, metadata) {
}
}
};
let handler = Plaid.create(configs);
$('#link-button').on('click', function(e) {
handler.open();
});
})(jQuery);
</script>
<script id="plaid">
Parse.Cloud.run("plaidAPI")
</script>
</body>
</body>
</html>
这是我的云代码:
var bodyParser = require('body-parser');
var express = require('express');
var plaid = require('plaid');
// We store the access_token in memory - in production, store it in
// a secure persistent data store.
let ACCESS_TOKEN = null;
let ITEM_ID = null;
const client = new plaid.Client({
clientID: 'Hidden-For-Obvious-Reasons',
secret: 'Hidden-For-Obvious-Reasons',
env: plaid.environments.sandbox
});
const app = express();
// Create a link_token to initialize Link
app.post('/create_link_token', async function(request, response, next) {
// Grab the client_user_id by searching for the current user in your database
const user = await User.find(...);
const clientUserId = user.id;
// Create the link_token with all of your configurations
client.createLinkToken({
user: {
client_user_id: clientUserId,
},
client_name: 'My App',
products: ['transactions'],
country_codes: ['US'],
language: 'en',
webhook: 'https://sample.webhook.com',
}, function(error, linkTokenResponse) {
// Pass the result to your client-side app to initialize Link
response.json({ link_token: linkTokenResponse.link_token });
});
});
// Accept the public_token sent from Link
app.post('/get_access_token', function(request, response, next) {
const public_token = request.body.public_token;
client.exchangePublicToken(public_token, function(error, response) {
if (error != null) {
console.log('Could not exchange public_token!' + '\n' + error);
return response.json({error: msg});
}
// Store the access_token and item_id in your database
ACCESS_TOKEN = response.access_token;
ITEM_ID = response.item_id;
console.log('Access Token: ' + ACCESS_TOKEN);
console.log('Item ID: ' + ITEM_ID);
response.json({'error': false});
});
});
app.listen(8000);
}
【问题讨论】:
-
您首先需要决定是要创建cloud code function 还是custom express route。您当前正在创建自定义快速路由并像调用云代码函数一样调用它。
标签: javascript html node.js parse-platform plaid