Authenticating Twitter on Rails app - ruby-on-rails

I'm using the twitter gem along with amniauth to fetch a user's tweets and display them on my homepage. But when I try to grab tweets from the command line I'm getting a 'bad authentication data' error.
Gemfile:
gem 'omniauth'
gem 'omniauth-twitter'
gem 'twitter'
Twitter.rb in config/initializers:
Twitter.configure do |config|
config.consumer_key = 'key'
config.consumer_secret = 'secret'
config.oauth_token = 'token'
config.oauth_token_secret = 'secret'
end
omniauth.rb in config/initializers:
require 'omniauth-twitter'
config.omniauth :twitter, 'key', 'secret'
I've double checked my key and passwords, but I can't get it to run. I am using Spree. Here's the error:
irb(main):001:0> require 'twitter'
=> true
irb(main):002:0> Twitter.user_timeline(213747670)
Twitter::Error::BadRequest: Bad Authentication data
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/response/raise_error.rb:21:in `on_complete'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/response.rb:9:in `block in call'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/response.rb:63:in `on_complete'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/response.rb:8:in `call'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/request/url_encoded.rb:14:in `call'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/request/multipart.rb:13:in `call'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/request/multipart_with_file.rb:14:in `call'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/connection.rb:247:in `run_request'
from /usr/local/lib64/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/connection.rb:100:in `get'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/client.rb:81:in `request'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/client.rb:64:in `get'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/api/utils.rb:51:in `objects_from_response'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/api/utils.rb:42:in `objects_from_response_with_user'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/api/timelines.rb:53:in `user_timeline'
from /usr/local/lib64/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter.rb:52:in `method_missing'
from (irb):2
Thanks for your help.

To fetch and display user's tweets its better to go with twitter gem.
Refer the following links for step-by-step instructions
http://shouweick.blogspot.in/2011/11/rails-3-and-twitter-api-by-shouweick.html?showComment=1381925399790#c6571680143535387130
https://gist.github.com/fnichol/867550

Related

2.3.0/gems/twitter-6.1.0/lib/twitter/rest/response/raise_error.rb:13:in `on_complete': Bad Authentication data. (Twitter::Error::BadRequest)

Simply put, I'm attempting to post a tweet using these pieces of ruby code:
require 'Twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "my_key"
config.consumer_secret = "my_secret_key"
config.access_token = "my_token"
config.access_token_secret = "my_secret_token"
end
client.update("I'm tweeting with a #gem!")
I'm using 64-bit ruby 2.3.0 with the appropriate 64-bit dev kit on Windows 10.
However, a `on_complete': Bad Authentication data. (Twitter::Error::BadRequest) appears like so:
C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/response/raise_error.rb:13:in `on_complete': Bad Authentication data. (Twitter::Error::BadRequest)
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/response.rb:9:in `block in call'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/response.rb:61:in `on_complete'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/response.rb:8:in `call'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/request/url_encoded.rb:15:in `call'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/request/multipart.rb:14:in `call'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/request/multipart_with_file.rb:21:in `call'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/rack_builder.rb:139:in `build_response'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/connection.rb:377:in `run_request'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/faraday-0.11.0/lib/faraday/connection.rb:177:in `post'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/request.rb:33:in `perform'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/utils.rb:50:in `perform_request'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/utils.rb:64:in `perform_post_with_object'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib/twitter/rest/tweets.rb:158:in `update!'
from C:/tools/ruby23/lib/ruby/gems/2.3.0/gems/twitter-6.1.0/lib /twitter/rest/tweets.rb:128:in `update'
from om_tweet.rb:11:in `<main>'
Thanks in advance for your time and advice!
After copy/pasting the keys into their appropriate lines, whitespace was found to be present in the keys. Deleting the whitespace allowed the ruby file to function as designed. Thanks and kudos to Tom Lord for help and critique!

Twitter::Error::Forbidden: Error creating status

I am trying to send tweets from a web job to notify my users of certain events.
It works fine on my local machine but when deployed to Heroku I get the following error when trying to tweet with media:
Twitter::Error::Forbidden: Error creating status.
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/response/raise_error.rb:12:in `on_complete'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/response.rb:9:in `block in call'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/response.rb:57:in `on_complete'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/response.rb:8:in `call'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/request/url_encoded.rb:15:in `call'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/request/multipart.rb:14:in `call'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/request/multipart_with_file.rb:21:in `call'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/rack_builder.rb:139:in `build_response'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/connection.rb:377:in `run_request'
from /app/vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/connection.rb:177:in `post'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/request.rb:33:in `perform'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/utils.rb:50:in `perform_request'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/utils.rb:64:in `perform_post_with_object'
from /app/vendor/bundle/ruby/2.2.0/gems/twitter-5.17.0/lib/twitter/rest/tweets.rb:233:in `update_with_media'
I can tweet pure text tweets without any errors. Any ideas as to what may be the cause of this?
is there some constraints on Heroku that prevents me from sending the image?
The code sending the tweet looks like this:
def send_tweet(company, data)
file_name = ChartGenerator.build_company_chart(data)
client.update_with_media(company_tweet_text(company), File.new(file_name))
end
def send_test_tweet
client.update('Test tweet')
end
def client
Twitter::REST::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end
end
send_test_tweet works fine on Heroku and locally and send_tweet works locally but NOT on Heroku.
I have via console verified that the ChartGenerator works as expected on Heroku.
Could there be some Faraday settings needed for production? (I noticed faraday in the stacktrace)

Using mailjet in rails

I want to use Mailjet in order to send order confirmation email.
First of all, I installed Gem and configure as Mailjet guide
gem 'mailjet'
initializers/mailjet.rb
Mailjet.configure do |config|
config.api_key = '<my_api_key>'
config.secret_key = '<my_secret_key>'
config.default_from = '<my_email>'
end
config/application.rb
config.action_mailer.delivery_method = :mailjet_api
And this is action in my controller
def send_email_reserved
email = { :from_email => "<my_from_email>",
:from_name => "Dona Sky",
:subject => "Hello",
:text_part => "Hi",
:recipients => [{:email => "<my_to_email>"}] }
test = Mailjet::Send.create(email)
p test.attributes['Sent']
end
When I called the action, it showed the error
NoMethodError: undefined method `[]' for #<Set: {#<MIME::Type: application/json>}>
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:307:in `type_for_extension'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:312:in `type_for_extension'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `block (2 levels) in stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `map'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `block in stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `each'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `inject'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:92:in `make_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:58:in `initialize'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/resource.rb:67:in `post'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/connection.rb:67:in `handle_api_call'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/connection.rb:47:in `post'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/resource.rb:215:in `save'
This is a dependency issue. Try adding this : gem rest-client '~>1.6.9' to your Gemfile and run bundle update

Carrierwave/Fog-azure: azure is not a recognized provider (ArgumentError)

I'm trying to connect my rails 4 project to Azure, I use carrierwave and fog for managing and storing the images.
This is the error I get when starting the server or the console:
/Users/giulio/.rvm/gems/ruby-2.2.1#my_project/gems/fog-core-1.32.0/lib/fog/core/services_mixin.rb:12:in `new': azure is not a recognized provider (ArgumentError)
from /Users/giulio/.rvm/gems/ruby-2.2.1#my_project/gems/fog-core-1.32.0/lib/fog/storage.rb:22:in `new'
from /Users/giulio/.rvm/gems/ruby-2.2.1#my_project/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:83:in `eager_load_fog'
from /Users/giulio/.rvm/gems/ruby-2.2.1#my_project/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='
from /Users/giulio/Documents/rails/my_project/config/initializers/carrier_wave.rb:7:in `block in <top (required)>'
I have in my gemfile:
gem 'carrierwave'
gem 'fog'
gem 'fog-azure'
Carrierwave initializer is:
CarrierWave.configure do |config|
if Rails.env.test?
config.storage = :file
config.enable_processing = false
else
config.storage = :fog
config.fog_credentials = Rails.application.secrets.fog_credentials.symbolize_keys
config.fog_directory = "my_directory"
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'public, max-age=315576000'}
end
end
my secrets.yml contains:
fog_credentials:
provider: 'azure'
azure_sub_id: '12a2341c-22ac-1561-5ed2-17865d910ba4'
azure_pem: '~/secret.pem'
azure_api_url: 'usnorth.management.core.windows.net'
Checking into fog-azure gem code I figured that fog-azure deals only with the 'Compute' module of fog, while carrierwave uses the 'storage' module.
From this I understand that the fog-azure can only be used for managing servers (i.e. starting, stopping, provisioning) not with the storage
I found also carrierwave-azure gem to get carrierwave to support azure, I'll be trying this.

Ruby FOG and custom openstack provider: NotImplementedError

I'm using a Paperclip and Fog to work with cloud storage. Cloud storage provides Openstack API, and here is my setup:
paperclip_defaults.rb:
Paperclip::Attachment.default_options.update({
:path => "images/:class/:id/:attachment/:style/img_:fingerprint",
:storage => :fog,
:fog_credentials => {
:provider => 'OpenStack',
:openstack_auth_url => 'myurl',
:openstack_username => 'login',
:openstack_api_key => 'pass'
},
:fog_directory => 'dir',
:fog_public => true,
})
Everything works fine for upload, but I can't download a picture!
Here is stacktrace:
fog (1.23.0) lib/fog/openstack/models/storage/directory.rb:35:in `public_url'
fog (1.23.0) lib/fog/openstack/models/storage/files.rb:67:in `get_url'
fog (1.23.0) lib/fog/openstack/models/storage/file.rb:81:in `public_url'
paperclip (4.2.0) lib/paperclip/storage/fog.rb:136:in `public_url'
paperclip (4.2.0) lib/paperclip/storage/fog.rb:51:in `block (2 levels) in extended'
paperclip (4.2.0) lib/paperclip/interpolations.rb:34:in `block (2 levels) in interpolate'
paperclip (4.2.0) lib/paperclip/interpolations.rb:33:in `gsub'
paperclip (4.2.0) lib/paperclip/interpolations.rb:33:in `block in interpolate'
paperclip (4.2.0) lib/paperclip/interpolations.rb:32:in `each'
paperclip (4.2.0) lib/paperclip/interpolations.rb:32:in `inject'
paperclip (4.2.0) lib/paperclip/interpolations.rb:32:in `interpolate'
paperclip (4.2.0) lib/paperclip/url_generator.rb:13:in `for'
paperclip (4.2.0) lib/paperclip/attachment.rb:142:in `url'
NotImplementedError
public_url always returns this error for openstack storage. What's wrong?
You need to set "fog_host" to your object storage service endpoint.
Here is an example.
https://gist.github.com/withhawaii/4527bfdbe4fd1d52f67a
You can find your endpoint in Openstack Horizon.
Login to Horizon and click "Access & Security" => "API Access".
You will find the URL for Object Storage service endpoint.
It's basically an URL, where your users can access files.
Your system administrator may set up a proxy or something.
If that's the case, set the URL your system administrator gave to you.
Hope this will help.
GT

Resources