【问题标题】:Python Stripe SDK Authentication error (key contains at least one space)Python Stripe SDK Authentication 错误(key 至少包含一个空格)
【发布时间】:2017-11-29 00:05:31
【问题描述】:

我正在尝试使用 Python SDK 在我的 Stripe 帐户中复制一些订阅计划,以便我可以更新计划价格(计划是不可变的)。我可以成功列出所有计划,因此身份验证不是问题。:

import stripe

stripe.api_key = 'sk_test_...'
start_id = None

while True:
    if start_id:
        resp = stripe.Plan.list(starting_after=start_id)
    else:
        resp = stripe.Plan.list()

    plans = resp['data']
    if len(plans) == 0: break
    start_id = plans[-1]['id'] 

    for plan in plans:
        new_amount = get_new_plan_amount(plan['id'], plan['name'])
        new_plan = {
            "id": "%s-v2" % plan["id"],
            "name": "%s V2" % plan["name"],
            "amount": new_amount,
            "interval": plan["interval"],
            "currency": plan["currency"],
        }
        if plan['interval_count']:
            new_plan["interval_count"] = plan['interval_count']

        if plan['metadata']:
            new_plan["metadata"] = plan['metadata']

        if plan['statement_descriptor']:
            new_plan["statement_descriptor"] = plan['statement_descriptor']

        stripe.Plan.create(new_plan) ### error

当我尝试更新计划时出现以下错误:

Stripe.error.AuthenticationError:提供的 API 密钥无效:“{'****': *'*********** ', '******** ': *'*****', '********_*****': *, '********': '', '******': *****, '**': *'*******-v2'}”。该键至少包含一个空格。请删除空格,然后重试。

我不明白。哪个字段包含空格?我检查了id 字段(它似乎在暗示),但该字段中没有空格。

我正在使用 Python 2.7 和 Stripe API 版本 2013-08-13。

【问题讨论】:

    标签: python stripe-payments


    【解决方案1】:

    创建计划时需要解压包含参数的字典:

    stripe.Plan.create(**new_plan)
    

    此外,您不需要自己管理分页参数。 Python 库可以使用auto-pagination 为您完成这项工作:

    plans = stripe.Plan.list()
    for plan in plans.auto_paging_iter():
        # do something with plan
    

    【讨论】:

    • 当您说解包时,您的意思是该函数需要一个 JSON 字符串作为参数吗?
    • stripe.Plan.create() 方法需要关键字参数,而不是字典,因此您需要解压缩参数列表:docs.python.org/dev/tutorial/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    相关资源
    最近更新 更多