【问题标题】:How does one retrieve the public_token for Plaid API in Python如何在 Python 中检索 Plaid API 的 public_token
【发布时间】:2020-06-06 13:50:22
【问题描述】:

我正在尝试关注 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,他们会说:

此存储库现已弃用。要与 Plaid 集成,请访问 文档。 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

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

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

access_token 用于访问项目的产品数据。这 应该安全地存储,而不是在客户端代码中。这是使用 为用户向 Plaid API 发出经过身份验证的请求。经过 默认情况下,access_tokens 不会过期,但您可以轮换它们;如果 它以错误状态结束,如果访问令牌将再次工作 项目的错误已解决。每个 access_token 对特定的 项,不能用于访问其他项。

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

【问题讨论】:

    标签: python-3.x plaid


    【解决方案1】:

    基本上public_token来自客户端手段

    当您将实现该 Plaid 链接或创建项目的前端部分/客户端时,然后创建 public_token here is the link for that (official-docs) 的后端进程; 根据公共令牌生成的 onSuccess 上的文档;

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

    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']
    

    【讨论】:

    • 最终发现了这一点,但仍然有(任何)方法可以在没有 client 端的情况下做到这一点吗?我的意思是,我只想围绕他们的 API 构建一个纯粹的自定义 API 包装器。
    • 我也对如何获取公共令牌感到困惑。我翻阅了 Plaid 的 QuickStart 示例,发现 Plaid 客户端库以某种方式生成它:github.com/plaid/quickstart/blob/…。和你一样,我不需要 Plaid 前端,我想从服务器端获取公共令牌。我还没有找到一种方法来做到这一点,但正在寻求 Plaid 支持。
    • @jones-chris 你有想过这个吗?在同样的情况下,尽量避免使用格子的前端
    • @dan_g 我联系了 Plaid,他们说我必须使用他们的前端 :(。如果您正在寻找一种以编程方式提取银行数据的方法,您可以使用 Python 和 OFX。请参阅此示例:github.com/jones-chris/personal-budget/blob/master/…。它在我的本地机器上成功运行,但在 AWS 中不起作用 - 我的猜测是因为 USAA 客户端白名单....但我不确定。我希望这会有所帮助 :)!
    猜你喜欢
    • 2022-11-04
    • 2017-02-01
    • 2021-12-05
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多