ruby-on-railsherokumigrationopen-uri

500 error after pushing to heroku - DEBUG


Im having an openuri issue when in production. The code below works in development but when in production the unauthorized error comes up. The only way i've solved this is if i put the api key directly in the url, which obviously i dont want to do. Any ideas why my current code doesnt work?

 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)


Solution

  • OpenURI returns a 401 error and my guess is that the ENV['NEWS_API'] is not set. You can run heroku config in the terminal or look it up on the web interface: Settings -> Reveal Config Vars