Stripe::AuthenticationError没有提供API key



使用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]

应用程序比;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.rb初始化的情况下运行?

您可以在请求处理程序中打印p Stripe.api_key作为诊断,以确保它已被设置。但是,请确保它没有在任何地方记录日志,并且更倾向于使用测试键。

您的环境变量可能有问题,请在您的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密钥:

证书文件

希望有帮助。

最新更新