推送到调试后出现 500 错误



我在生产中遇到openuri问题。下面的代码在开发中有效,但在生产中会出现未经授权的错误。我解决这个问题的唯一方法是将 api 密钥直接放在 url 中,这显然是我不想做的。知道为什么我当前的代码不起作用吗?

api_key = ENV["NEWS_API"]
url = "https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=#{api_key}"
article_serialized = open(url).read
@articles = JSON.parse(article_serialized)

2018-10-25T01:55:47.184856+00:00 app[web.1]: F, [2018-10-25T01:55:47.184804 #4] FATAL -- : [40a7ee38-ef6e-4622-ad86-edb34d7eeccf] OpenURI::HTTPError (401 Unauthorized):

Running rails db:migrate on ⬢ twittter-clone... up, run.6483 (Free)
D, [2018-10-25T01:12:44.387465 #4] DEBUG -- :    (0.9ms)  SELECT pg_try_advisory_lock(2661719123600558280)
D, [2018-10-25T01:12:44.433095 #4] DEBUG -- :    (2.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
D, [2018-10-25T01:12:44.456835 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (3.8ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
D, [2018-10-25T01:12:44.480937 #4] DEBUG -- :    (5.3ms)  BEGIN
D, [2018-10-25T01:12:44.485057 #4] DEBUG -- :    (1.7ms)  COMMIT
D, [2018-10-25T01:12:44.492822 #4] DEBUG -- :    (7.1ms)  SELECT pg_advisory_unlock(2661719123600558280)

OpenURI 返回 401 错误,我的猜测是未设置ENV['NEWS_API']。您可以在终端中运行heroku config或在 Web 界面上查找:Settings->Reveal Config Vars

相关内容

  • 没有找到相关文章

最新更新