如何在 Python 中检索格子 API 的 public_token

How does one retrieve the public_token for Plaid API in Python

我正在尝试遵循 Plaid 的文档 from here,但当我在 Plaid 上工作时,我似乎无法弄清楚从哪里获得 public_token开发 环境。他们是这样说的:

在他们的第二个例子中:

from plaid import Client


client = Client(client_id='***', secret='***', public_key='***', environment='sandbox')

# the public token is received from Plaid Link
response = client.Item.public_token.exchange(public_token)
access_token = response['access_token']

我不知道 public_token 来自哪里。如果我去 Plaid Link 他们说:

This repository is now deprecated. To integrate with Plaid, visit the docs. https://plaid.com/docs

然后,当我去他们的 official docs 时,我似乎找不到任何方法来获得 public token

我正在尝试按照这些思路做一些事情:

from exceptions import (
    InvalidPlaidEnvironment,
    MissingPlaidCredential
)

from plaid import Client
from plaid.errors import APIError, ItemError, InvalidRequestError


VALID_ENVIRONMENTS = (
    'sandbox',
    'development',
    'production'
)

CLIENT_ID = '<development_client_id>'
SECRET = '<development_secret>'
PUBLIC_KEY = '<development_public_key>'
ENVIRONMENT = 'development'


class PlaidProcessor:
    """
    Simple processor class for Plaid API
    """

    def __init__(
            self,
            client_id=CLIENT_ID,
            secret=SECRET,
            public_key=PUBLIC_KEY,
            environment=ENVIRONMENT
    ):
        """
        Constructor for PlaidProcessor. Initialize our class with basic credentials
        data.

        Args:
            client_id (str): Plaid client ID
            secret (str): Plaid secret
            public_key (str): Plaid public key
            environment (str): One of sandbox, development, or production
        """

        self.client_id = client_id
        self.secret = secret
        self.public_key = public_key
        self.environment = environment

        if not self.client_id:
            raise MissingPlaidCredential(
                'Missing CLIENT_ID. Please provide Plaid client id.'
            )

        if not self.secret:
            raise MissingPlaidCredential(
                'Missing SECRET. Please provide Plaid secret.'
            )

        if not self.public_key:
            raise MissingPlaidCredential(
                'Missing PUBLIC_KEY. Please provide Plaid public key.'
            )

        if not self.environment:
            raise MissingPlaidCredential(
                'Missing environment. Please provide Plaid environment.'
            )

        if self.environment.lower() not in VALID_ENVIRONMENTS:
            valid_environments_str = ','.join(VALID_ENVIRONMENTS)

            raise InvalidPlaidEnvironment(
                f'Please insert one of the following environments: {valid_environments_str}'
            )

        self.client = Client(
            client_id=self.client_id,
            secret=self.secret,
            public_key=self.public_key,
            environment=self.environment
        )

        self.access_token = None
        self.public_token = None

    def get_public_token(self):
        # how do I get the access token?
        pass

在文档中,他们还指定了以下内容:

A public_token (which is returned in your Link onSuccess() callback) should be passed to your server, which will exchange it for an access_token. public_tokens are one-time use tokens and expire after 30 minutes. You can generate new public_tokens as needed via the /item/public_token/create endpoint.

An access_token is used to access product data for an Item. This should be stored securely, and never in client-side code. This is used to make authenticated requests to the Plaid API for the user. By default, access_tokens do not expire, though you can rotate them; if it winds up in an error state, the access_token will work again if the Item’s error is resolved. Each access_token is unique to a specific Item, and cannot be used to access other Items.

但这更令人困惑。有人可以就此给我一些建议吗?

基本上public_token来自客户端意味着

当您将 frontend-part/client-side 的 Plaid 实施到 Link 或创建项目时,那么其背后的过程将创建 public_token here is the link for that (official-docs); 根据 public 令牌生成的 onSuccess 上的文档;

并且 public 令牌将发送到 server-side/backend-part here,您可以根据要求使用该 public 令牌:
如下:

from plaid import Client


client = Client(client_id='***', secret='***', public_key='***', environment='sandbox')

# the public token is received from Plaid Link
response = client.Item.public_token.exchange(public_token)
access_token = response['access_token']