问题描述
开发人员的新手,实际上只是将格子链接集成(https://plaid.com/docs/#integrating-with-link)复制并粘贴到客户端代码的基本HTML页面中,并复制到服务器端代码的back4app的云代码(Node JS)中,从云端调用该函数,但什么也没有发生,但出现以下错误:
- 提取API无法加载file:/// create_link_token。要进行CORS请求,URL方案必须为“ http”或“ https”。
- index2.html:42未捕获(按承诺)TypeError:无法获取 在fetchLinkToken(index2.html:42)
- parse.min.js:13 POST https://parseapi.back4app.com/functions/plaidAPI 400
- parse.min.js:13未捕获(承诺)错误:无效的函数:“ plaidAPI” 在handleError
我的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,next) {
const public_token = request.body.public_token;
client.exchangePublicToken(public_token,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);
}
解决方法
需要通过快速路由启动节点,而不是调用云代码。