如何在 Python 中检索 Plaid API 的 public_token

Gra*_*ex. 4 python-3.x plaid

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

在他们的第二个例子中:

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']
Run Code Online (Sandbox Code Playgroud)

我不知道从哪里来的public_token。如果我去那个格子链接他们说:

此存储库现已弃用。要与 Plaid 集成,请访问文档。https://plaid.com/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
Run Code Online (Sandbox Code Playgroud)

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

A public_token(在您的 LinkonSuccess()回调中返回)应该传递给您的服务器,服务器会将其交换为 access_token. public_tokens 是一次性使用的令牌,30 分钟后过期。您可以根据需要通过 /item/public_token/create 端点生成新的 public_token。

Anaccess_token用于访问项目的产品数据。这应该安全地存储,并且永远不要存储在客户端代码中。这用于为用户向 Plaid API 发出经过身份验证的请求。默认情况下,access_tokens 不会过期,但您可以轮换它们;如果它最终处于错误状态,并且项目的错误得到解决,则 access_token 将再次工作。每个 access_token 对特定 Item 是唯一的,不能用于访问其他 Item。

但这更令人困惑。任何人都可以给我一些关于这方面的建议吗?

小智 7

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

当您为该 Plaid 实现前端部分/客户端以链接或创建项目时,创建public_token 的后端 流程是该链接(官方文档);根据公共令牌生成的onSuccess文档;

并且该公共令牌将发送到此处的服务器端/后端部分,您可以根据要求使用该公共令牌:
如下:

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']
Run Code Online (Sandbox Code Playgroud)

  • @dan_g 我联系了 Plaid,他们说我必须使用他们的前端:(。如果您正在寻找一种编程方式来提取银行数据,您可以使用 Python 和 OFX。请参阅此示例:https://github.com /jones-chris/personal-budget/blob/master/personal_budget/main.py。它在我的本地计算机上成功运行,但在 AWS 中不起作用 - 我的猜测是因为 USAA 客户端白名单......但我不确定。我希望这有帮助:)! (2认同)