I have this code as an initializer in yelp.rb:
Yelp.client.configure do |config|
config.consumer_key = ENV['config.consumer_key']
config.consumer_secret = ENV['config.consumer_secret']
config.token = ENV['config.token']
config.token_secret = ENV['config.token_secret']
end
I have a yelp.yml file that loads all this in and it works great in development.
As soon as I push it to heroku (I have all my keys set in Heroku as well and have triple verified no spelling errors) I get this error 'Yelp::Error::MissingAPIKeys: You're missing an API key'
I've ran code in Rails C on development (see below) and it passes, ran the exact same code in Rails c on the Heroku side and I get that error. I even tried it without using ENV and used the exact api keys and same error.
client = Yelp::Client.new({
consumer_key = ENV['config.consumer_key'],
consumer_secret = ENV['config.consumer_secret'],
token = ENV['config.token'],
token_secret = ENV['config.token_secret'] })
What is different between Production and Development?
UPDATE:
Got it working...
I was able to get it working this way...
def index
current_user.zip_code.present? ? #zip = current_user.zip_code : #zip = "94101"
parameters = { term: 'auto repair', limit: 9 }
#search = client.search(#zip, parameters)
end
private
def client
#client ||= Yelp::Client.new({ consumer_key: ENV['config.consumer_key'],
consumer_secret: ENV['config.consumer_secret'],
token: ENV['config.token'],
token_secret: ENV['config.token_secret']
})
end
Run heroku config --app app-name to check if config variables are being set correctly, if that looks correct. Then try running rails console on Heroku using heroku run rails console --app app-name to check if the Yelp::Client is loading the env properly.
If you just follow the ENV naming convention as provided in the gem readme you might avoid this issue altogether.
Yelp.client.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.token = YOUR_TOKEN
config.token_secret = YOUR_TOKEN_SECRET
end
Related
I'm currently working on sharpening my RoR skills after spending three months learning the basics through a bootcamp. I'm slowly building concept upon concept with an app I'm working on to create a Twitter utility (been done, I know. It's been good practice).
After much trepidation I was able to get oauth-twitter up and running but now I'm starting to scratch the surface of the Twitter API and I'm having some difficulties.
I've created what I believe is all the functionality to send tweets directly from my application but, upon hitting send I get this error:
NameError at /tweets
undefined local variable or method 'oauth_token' for #<User:0x007f821a6b66d0>`
It's throwing it off this block of code:
def tweet(tweet)
client = Twitter::REST::Client.new do |config|
config.consumer_key = Rails.application.config.twitter_key
config.consumer_secret = Rails.application.config.twitter_secret
config.access_token = oauth_token
config.access_token_secret = oauth_secret
end
client.update(tweet)
end
end
With the error being highlighted at config.access_token = oauth_token
The server stack trace gives me this:
NoMethodError - undefined method `consumer_key' for # <Rails::Application::Configuration:0x007f82182b9188>:
railties (4.2.3) lib/rails/railtie/configuration.rb:95:in `method_missing'
app/models/user.rb:16:in `block in tweet'
twitter (5.15.0) lib/twitter/client.rb:23:in `initialize'
app/models/user.rb:15:in `tweet'
If anyone could take a look at this and give me some feedback I'd much appreciate it. You can check out the branch in my repo where this is all stored here: InsomniaNoir - Project: :kronoTweeter
Thanks in advance!
You have not both oauth_token and oauth_secret in users table. You need create new migration like this:
rails g migration AddOauthFieldsToUsers oauth_token oauth_secret
after this don't forget run
rake db:migrate
then should be modified from_omniauth method in User class
def from_omniauth(auth_hash)
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
user.name = auth_hash['info']['name']
user.location = auth_hash['info']['location']
user.image_url = auth_hash['info']['image']
user.url = auth_hash['info']['urls']['Twitter']
user.oauth_token=auth_hash.credentials.token
user.oauth_secret=auth_hash.credentials.secret
user.save!
user
end
I'm trying to integrate my app with twitter API.
here are my steps:
1: installed the twitter gem: gem 'twitter
2:grabbed this code from the twitter gem documentation and placed it inside my controller. I also tried placing the code in a helper.
require 'twitter'
#client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
config.access_token = ENV["ACCESS_TOKEN"]
config.access_token_secret = ENV["ACCESS_SECRET"]
end
3) registered my app with twitter and got the consumer_key (AKA: API key), consumer_secret (AKA:API secret), access_token, and access_token_secret.
I used a config/application.yml to store access tokens. is that correct way of doing it? I hope so.
4) in views, I have <%= #client.user.username %>.
I'm doing this in development (localhost3000). my callback url is http://127.0.0.1/
I also tried to organize my code as this answer suggests to no avail.
I'm using Rails 4.0.2
I tested the code in the console and is working perfectly. I tried several queries including the one shown above: <%= #client.user.username %> and they are all working.
but when I run my code in development i get this error:
undefined method `user'` for nil:NilClass.
I understand what the error means. but how is it possible that #client is nil? when I run it in the console, it is not nil. Did I forget something? I would appreciate any suggestions. Please let me know if you would like me to provide more info/code. Thanks.
EDIT: Also, tried to move the above code that start with #client to a config/initializers/twitter.rb. Getting same error message.
This may be because your controller code is trying to access CONSUMER_KEY etc. from environment variable but you have not set it in development mode.
You can use rails_config gem and let it create the settings.yml file for you in config/ with appropriate configurations.
] Now, store you twitter details in it as follows:
twitter:
CONSUMER_KEY: 'your_consumer_key_here'
CONSUMER_SECRET: 'your_consumer_secret_here'
ACCESS_TOKEN: 'your_access_token_here'
ACCESS_SECRET:'your_access_secret_here'
Then, modify your controller code to use those values as follows:
account = Settings['twitter']
#client = Twitter::REST::Client.new do |config|
config.consumer_key = account["CONSUMER_KEY"]
config.consumer_secret = account["CONSUMER_SECRET"]
config.access_token = account["ACCESS_TOKEN"]
config.access_token_secret = account["ACCESS_SECRET"]
end
You can add settings.yml to .gitignore so that the file is not commited by git.
Or, you can use the approach described Here.
You can read this if you are thinking of setting ENV variables in development.
I think your keys are not getting loaded properly
your config/settings.yml should look like this
development:
twitter:
CONSUMER_KEY: 'your_consumer_key_here'
CONSUMER_SECRET: 'your_consumer_secret_here'
ACCESS_TOKEN: 'your_access_token_here'
ACCESS_SECRET:'your_access_secret_here'
make this file as per your environments
Then Load this as
def get_twitter_client
oauth_config = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
#client = Twitter::REST::Client.new do |config|
config.consumer_key = oauth_config[Rails.env]['twitter']["CONSUMER_KEY"]
config.consumer_secret = oauth_config[Rails.env]['twitter']["CONSUMER_SECRET"]
config.access_token = oauth_config[Rails.env]['twitter']["ACCESS_TOKEN"]
config.access_token_secret = oauth_config[Rails.env]['twitter']["ACCESS_SECRET"]
end
end
Also if you look at Twitter gem #client has method user(user show API) which accepts user_id or twitter_username/handle/screen_name
I tried both solutions outlined here by #Pramod Shinde and #Peeyush, but none of them seemed to work for me. Here is how I solved the problem.
What I needed to do is to wrap a method action around the following code which I had inside my controller. Like this:
def tweets
#client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
config.access_token = ENV["ACCESS_TOKEN"]
config.access_token_secret = ENV["ACCESS_SECRET"]
end
end
I created a new file, config/application.yml to store my twitter API credentials, and added the file to .gitignore to avoid it being commited to github. The above code uses ENV[] to access the credentials stored in config/application.yml.
config/application.yml:
CONSUMER_KEY: "1234"
CONSUMER_SECRET: "1234"
ACCESS_TOKEN: "1234"
ACCESS_SECRET: "1234"
This is a YAML file. So beware of the indentation.
Then inside my routes.rb, I created a route that maps to the above action:
get "/tweets", to: "posts#tweets"
Obviously, if you wanted to post tweets instead of fetching them, you'd have to create a POST route. You may also consider moving the above action to it's own TweetsController. Or encapsulate it in it's own class.
To complete the convention, I created a view file which maps to the tweets action:
tweets.html.erb
The reason why I was getting this error message:
undefined method `user' for nil:NilClass
is because I didn't have a route, controller action, and a view file that map to each.
Up until this point, everything is working fine when in development.
when I pushed the app to heroku, I got the following error:
ActionView::Template::Error (Unable to verify your credentials):
Solution: install gem 'figaro' (actually this gem creates for you config/application.yml file and automatically appends it to .gitignore.
To solve the heroku error, all you have to do is run: rake figaro:heroku. See this tutorial about using the figaro gem to keep the environment variables private
I've been trying to get the Twitter Gem running. I followed the documentation and placed
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
I placed it in a custom initializer file called Twitter.rb
When I try to run rails console it gives me this error " uninitialized constant Twitter::REST (NameError)"
This error was reported here
The solution is to use Twitter::Client.new (no ::REST)
I faced with same issue. But I have installed two gems https://github.com/sferik/twitter and https://github.com/twitter/twitter-text-rb, they both have 'Twitter' as main module and when I call Twitter, I reached second instead first...
Twitter::Client.new - not working to.
add require 'twitter' on the top of your file
I'm building a rudimentary app using the Twilio API, and have reached a block when using figaro to store account_sid and auth_token as environment variables in application.yml.
In my controller I have:
require 'twilio-ruby'
require 'figaro'
class TwilioController < ApplicationController
def voice
account_sid = ENV["TWILIO_ACCOUNT_SID"]
auth_token = ENV["TWILIO_AUTH_TOKEN"]
#client = Twilio::REST::Client.new account_sid, auth_token
message = #client.account.sms.messages.create(:body => "Hello",
:to => "+12345678",
:from => "+12345678")
puts message.sid
end
end
And in config/application.yml I have:
# TWILIO_ACCOUNT_SID: 1234567890
# TWILIO_AUTH_TOKEN: 1234567890
The program works as intended when I replace the env variables with actual values, so the most I'm able to grasp at the moment is that something is preventing those variables from being set.
I just upgraded from Rails 4.2 to Rails 5 and started getting this error.
For me, it was because a newer HAML version was no longer tolerant of a pre-existing empty javascript block.
:javascript
I deleted the ':javascript' line and everything was fine afterwards.
move constants to development.rb
and then just:
account_sid = TWILIO_ACCOUNT_SID
I am trying to get Carrierwave (0.5.1) to work with Mongoid (2.0.0.beta.20), Rails 3. I followed every step at this guide.
In config/initializers/carrierwave.rb, I have:
CarrierWave.configure do |config|
config.grid_fs_database = Mongoid.database.name
config.grid_fs_host = Mongoid.config.master.connection.host
config.storage = :grid_fs
config.grid_fs_access_url = "/uploads"
end
When I try to start my server (rails server). In the console, I get:
...config/initializers/carrierwave.rb:3:in `block in <top
(required)>': undefined method `host' for #<Mongo::Connection:
0x00000103802420> (NoMethodError)
I don't understand why I am getting this error. I've looked everywhere
and can't seem to locate why this is happening...
It seems, Mongoid.config.master.connection.host doesn't work anymore
in newer versions of Mongoid. Was this removed? What is the
replacement for this?
So far my workaround is the following code:
CarrierWave.configure do |config|
config.grid_fs_database = Mongoid.database.name
config.grid_fs_host = 'localhost'
config.storage = :grid_fs
config.grid_fs_access_url = "/uploads"
end
Line 3, should be: config.grid_fs_host = 'localhost'. <-- Is there a better way to dynamically indicate the host depending on environment?
Found out that the mongo gem has changed. So it now has to be:
config.grid_fs_host = Mongoid.database.connection.primary_pool.host