如何解决链接令牌如何返回公共令牌?公共令牌 = request.body.publictoken?
- 您能否解释一下 link_token 返回公共令牌的方式和位置?
app.post('/api/create_link_token',async function (request,response) {
const configs = {
user: {
// This should correspond to a unique id for the current user.
client_user_id: 'user-id',},client_name: 'Plaid Quickstart',products: PLAID_PRODUCTS,country_codes: PLAID_COUNTRY_CODES,language: 'en',};
if (PLAID_REDIRECT_URI !== '') {
configs.redirect_uri = PLAID_REDIRECT_URI;
}
if (PLAID_ANDROID_PACKAGE_NAME !== '') {
configs.android_package_name = PLAID_ANDROID_PACKAGE_NAME;
}
try {
const createTokenResponse = await client.linkTokenCreate(configs);
console.log('Create Token response below')
prettyPrintResponse(createTokenResponse.data);
response.json(createTokenResponse.data);
} catch (error) {
prettyPrintResponse(error);
return response.json(formatError(error.response));
}
});
根据 Plaid 文档: /link/token/create 端点创建了一个link_token,它在初始化Link 时需要作为参数。 Link 初始化完成后,它会返回一个 public_token,然后可以通过 /item/public_token/exchange 作为主 Link 流程的一部分将其交换为 access_token。
- PUBLIC_TOKEN= request.body.public_token 怎么样?公共令牌是如何进入 request.body 的?
app.post('/api/set_access_token',response,next) {
// Im inquiring about the line right below//
PUBLIC_TOKEN = request.body.public_token;
console.log("after",PUBLIC_TOKEN)
try {
const tokenResponse = await client.itemPublicTokenExchange({
public_token: PUBLIC_TOKEN,});
prettyPrintResponse(tokenResponse.data);
ACCESS_TOKEN = tokenResponse.data.access_token;
ITEM_ID = tokenResponse.data.item_id;
response.json({
access_token: ACCESS_TOKEN,item_id: ITEM_ID,error: null,});
} catch (error) {
prettyPrintResponse(error);
return response.json(formatError(error.response));
}
});
解决方法
- 链接令牌正在返回
response.json(createTokenResponse.data);
此代码使用 Express 框架——您可以在此处阅读更多相关信息:https://masteringjs.io/tutorials/express/json
在客户端,createTokenResponse.data
被接收为 data
并从中提取 link_token
:
` $.post("/api/create_link_token",{},function (data) {
...
localStorage.setItem("link_token",data.link_token);
handler = Plaid.create({
token: data.link_token,
或者,在 React Quickstart 中,在 frontend/src/App.tsx 中:
const generateToken = useCallback(
async (paymentInitiation) => {
const path = paymentInitiation
? "/api/create_link_token_for_payment"
: "/api/create_link_token";
const response = await fetch(path,{
method: "POST",});
...
const data = await response.json();
...
dispatch({ type: "SET_STATE",state: { linkToken: data.link_token } });
}
localStorage.setItem("link_token",data.link_token); //to use later for Oauth
},[dispatch]
);
-
public_token
在调用/api/set_access/token
期间设置,此处:
handler = Plaid.create({
token: data.link_token,onSuccess: function (public_token) {
$.post(
"/api/set_access_token",{
public_token: public_token
},
或者,如果您使用的是 React Quickstart,则此代码位于
frontend/src/Components/Link/index.tsx:
const onSuccess = React.useCallback(
(public_token: string) => {
// send public_token to server
const setToken = async () => {
const response = await fetch("/api/set_access_token",{
method: "POST",headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",},body: `public_token=${public_token}`,});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。