【问题标题】:Stripe::AuthenticationError No API key providedStripe::AuthenticationError 未提供 API 密钥
【发布时间】:2021-05-25 01:14:32
【问题描述】:

使用 Rails 6.1.3 、Ruby 3.0 + Stripe,但即使我认为我正确包含了所有键,我仍然会收到此错误:

配置 > 初始化程序 > stripe.rb

Rails.configuration.stripe = {
:publishable_key => Rails.application.secrets.publishable_key,
:secret_key => Rails.application.secrets.secret_key
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

app > secrets.yml

development:
publishable_key: "pk_test_51ILY7..."
secret_key: "sk_test_51ILY7..."

在终端中,一旦我调用 EDITOR=nano rails credentials:edit,这就是我所看到的:

stripe:
development:
publishable_key: 'pk_test_51ILY7...'
secret_key: 'sk_test_51ILY7...'
production:
publishable_key: 'pk_live_51ILY7...'
secret_key: 'sk_live_51ILY7...'

我可以在 Rails 终端中看到的错误

Started POST "/checkout/create_order" for ::1 at 2021-02-22 18:25:10 +0100
Processing by CheckoutController#create_order as JS
Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"1"}
Order Load (0.9ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/checkout_controller.rb:6:in `create_order'
Completed 500 Internal Server Error in 24ms (ActiveRecord: 0.9ms | Allocations: 1751)

Stripe::AuthenticationError (No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support@stripe.com if you have any questions.):

任何见解问题出在哪里?

谢谢

【问题讨论】:

  • 这就是没有缩进的秘密文件的样子吗?如果是这种情况,那么它应该将条带读取为“”。其他行应缩进。此外,您还需要调用生产哈希。 stripe[:production][:secret_key]。通过 dotenv 和您的托管服务提供商将这些添加为环境变量可能会更好。

标签: ruby-on-rails stripe-payments ruby-on-rails-6.1


【解决方案1】:

您需要确保环境变量真正被初始化。控制器是否可能在没有 stripe.rb 初始化的情况下运行?

您可以在请求处理程序中打印p Stripe.api_key 作为诊断,以确保已设置它。不过,请确保它没有在任何地方记录日志,并且更喜欢使用测试密钥。

【讨论】:

    【解决方案2】:

    您的环境变量可能有问题,请在您的 Stripe.rb 文件中尝试以下操作:

    module YourApp
      class Application < Rails::Application
        config.after_initialize do
          # initialization code goes here
          Stripe.api_key = Rails.application.credentials[:stripe][:secret]
        end
      end
    end
    

    还要确保使用 VIM 编辑器在凭据文件中添加 API 密钥。在您的终端中,输入:EDITOR=vim rails credentials:edit,然后添加您的秘密和公共 API 密钥:

    Credentials file

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 2017-05-31
      • 2015-11-23
      • 1970-01-01
      • 2017-01-18
      • 2014-06-23
      • 2015-12-09
      • 2017-10-25
      • 2017-02-24
      相关资源
      最近更新 更多