【问题标题】:stripe.error.AuthenticationError: No API key provided. (HINT: set your API key using "stripe.api_key = <API-KEY>")stripe.error.AuthenticationError:未提供 API 密钥。 (提示:使用“stripe.api_key = <API-KEY>”设置您的 API 密钥)
【发布时间】:2020-10-22 18:45:12
【问题描述】:

我在运行条带代码时遇到问题。如图所示插入代码。它给出了如下所示的错误。 stripe.api_key 可用但仍然无法正常工作,我明白了:

stripe.error.AuthenticationError:未提供 API 密钥。 (提示:使用“stripe.api_key =”设置您的 API 密钥)。您可以从 Stripe Web 界面生成 API 密钥。有关详细信息,请参阅 https://stripe.com/api,如果您有任何问题,请发送电子邮件至 support@stripe.com。

import stripe

posts = Blueprint('posts',__name__)

STRIPE_PUBLIC_KEY = os.environ.get('STRIPE_PUBLIC_KEY')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY')
stripe.api_key = STRIPE_SECRET_KEY


"""
posts.config['STRIPE_PUBLIC_KEY'] = 'pk_test_**********************'
posts.config['STRIPE_SECRET_KEY'] = 'sk_test_**********************'
stripe.api_key = posts.config['STRIPE_SECRET_KEY']
"""


@posts.route("/cart", methods=['GET','POST'])
def viewcart():
    stripe.api_key = STRIPE_SECRET_KEY
    session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        line_items=[{
        'price': 'prod_******',
        'quantity': 1,
        }],
        mode='payment',
        #success_url='https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
        #cancel_url='https://example.com/cancel',

        #success_url=url_for('thanks', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
        cancel_url=url_for('posts.viewcart', _external=True),
    )

【问题讨论】:

  • 你看过STRIPE_PUBLIC_KEY的内容了吗?你确定这些环境变量存在吗?
  • 是的,我有。我什至直接使用了密钥但不工作
  • 对不起,你的意思是你直接使用了密钥,它不起作用,还是直接使用但与代码一起使用时有效?
  • 我试过 ``` """ posts.config['STRIPE_PUBLIC_KEY'] = 'pk_test_************************' posts.config['STRIPE_SECRET_KEY'] = 'sk_test_**********************' stripe.api_key = posts.config['STRIPE_SECRET_KEY'] """ ` ``但仍然无法正常工作
  • 您确定您拥有的密钥是有效的吗?

标签: python flask stripe-payments


【解决方案1】:

这个问题是你这样声明stripe.api_key

stripe.api_key = STRIPE_SECRET_KEY

应该是:

stripe.api_key = current_app.config['STRIPE_SECRET_KEY']

另外,请记住包含from flask import current_app

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 2015-12-09
    • 2023-02-22
    • 2021-05-25
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多