Twitter Rails gem "Unable to verify your credentials", Initializer not working - ruby-on-rails

I'm using the Twitter Gem and Figaro but my credentials aren't being stored. Here's my setup:
config/initializers/twitter.rb
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_SECRET"]
end
config/application.yml:
TWITTER_CONSUMER_KEY: "12345"
TWITTER_CONSUMER_SECRET: "12345"
TWITTER_ACCESS_TOKEN: "12345"
TWITTER_ACCESS_SECRET: "12345"
Placing the below in a View results in a "Unable to verify your credentials" error which I believe is caused by the initializer not correctly storing my credentials.
<%=
#client = Twitter::REST::Client.new
#client.user_timeline("cnn") %>
I've tried putting various items in a Controller but nothings works. Via console:
client = Twitter::REST::Client.new
=> #<Twitter::REST::Client:0x007fadf06364b0>
client.consumer_key
=> nil
Any help would be much appreciated, thanks

client = Twitter::REST::Client.new is creating a new object with no params; it is not reusing what has been configured in config/initializers/twitter.rb
Try putting the initialization code & the accessing code in the controller together as follows:
#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_SECRET"]
end
#client.user_timeline("cnn")

Related

rails recaptcha error: We detected that your site is not verifying reCAPTCHA solutions. it return false all the time

I have this recaptcha error We detected that your site is not verifying reCAPTCHA solutions. This is required for the proper use of reCAPTCHA on your site. Please see our developer site for more information.
I did everything exactly but its still there recaptcha checkbox return false even after I checked the button
Here is my codes:
In gemfile
gem "recaptcha", require: "recaptcha/rails"
in config in in initializer in recaptcha.rb
Recaptcha.configure do |config|
config.site_key = 'my site key'
config.secret_key = 'my secret key'
end
In view:
<%= recaptcha_tags %>
In controller:
def create
#contact = Contact.new(contact_attributes)
if verify_recaptcha(model: #contact) && #contact.save
ContactMailer.message_send(#contact).deliver
redirect_to contacts_path, notice: "Thank you... Your Message was sent successfully."
else
flash.now[:error] = "Please correct the form"
render :index
end
end
I even try this in recaptcha.rb
Recaptcha.configure do |config|
config.public_key = ENV["RECAPTCHA_PUBLIC_KEY"]
config.private_key = ENV["RECAPTCHA_PRIVATE_KEY"]
end
but I got this error
undefined method `public_key=' for #<Recaptcha::Configuration:0x00007fc132b363e8> (NoMethodError)
Did you mean? public_send
In this link https://www.google.com/recaptcha/api/siteverify
I have
{
"success": false,
"error-codes": [
"missing-input-response",
"missing-input-secret"
]
}
Not sure what to do please help me and thanks in advance
Where did you put <%= recaptcha_tags %>?
As per: https://github.com/ambethia/recaptcha/blob/master/lib/recaptcha/configuration.rb
You can:
Recaptcha.configure do |config|
config.site_key = 'my site key'
config.secret_key = 'my secret key'
end
other wise the gem will automatically pick it from ENV['RECAPTCHA_SECRET_KEY'] and ENV['RECAPTCHA_SITE_KEY'].
This won't work:
Recaptcha.configure do |config|
config.public_key = ENV["RECAPTCHA_PUBLIC_KEY"]
config.private_key = ENV["RECAPTCHA_PRIVATE_KEY"]
end
My doubts now goes towards that you place recaptcha_tags in a wrong place (maybe out of the form). that it does not get sent to the controller/backend.

Rails: Disable Rollbar in development

I'm using Rollbar for error tracking in my Rails app. For some reason I'm getting errors from my localhost (in development). In config/initializers/rollbar.rb:
unless Rails.env.production?
config.enabled = false
end
Anything else I need to be doing?
Full rollbar.rb file:
Rollbar.configure do |config|
config.access_token = Figaro.env.rollbar_key
# Here we'll disable in 'test':
unless Rails.env.production?
config.enabled = false
end
end
This worked for me. Now you will get notified only when an exception happens in production.
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
if Rails.env.test? || Rails.env.development?
config.enabled = false
end
config.environment = ENV['ROLLBAR_ENV'].presence || Rails.env
end

How can I send tweets to my Twitter account?

Every time the user post a comments, I want it to send the same comment to my Twitter automatically.
First of all, I have it already done with Twitter developer settings.
So I made a test action in my App to make it send a tweet to my Twitter account.
However, it says this error
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/top_controller.rb:173:in `test_action'
How can I solve this? These are my codes
gems related that are already bundled (I'm on rails 3.2.11)
gem 'omniauth-twitter'
gem 'twitter'
gem 'figaro'
config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
Twitter.configure do |config|
config.consumer_key = ENV["TWITTER_KEY"]
config.consumer_secret = ENV["TWITTER_SECRET"]
end
end
config/application.yml
TWITTER_KEY: 6TeBX6HkeHzMXesgc
TWITTER_SECRET: JyfOndg8xHcM81KEpgmBT7h2vFJJujMP14YTdt6ruvLbsQk
test_action
def test_action
#twitter = Twitter::Client.new(oauth_token: request.env["omniauth.auth"][:credentials][:token], oauth_token_secret: request.env["omniauth.auth"][:credentials][:secret])
#twitter.update("Your message")
flash[:notice] = "Successfully tweeted on your account."
redirect_to root_path
return
end
I think your issue may be with your controller configuration of Twitter:
instead of:
#twitter = Twitter::Client.new(oauth_token: request.env["omniauth.auth"][:credentials][:token], oauth_token_secret: request.env["omniauth.auth"][:credentials][:secret])
try this:
#twitter = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['CONSUMER_KEY']
config.consumer_secret = ENV['CONSUMER_SECRET']
config.access_token = request.env["omniauth.auth"][:credentials][:token]
config.access_token_secret = request.env["omniauth.auth"][:credentials][:secret]
end
You also might want to confirm that your API keys have read and write permissions, which you can check on your Twitter developer account here.

Tweeting from Rails Application

I've set up Devise and Omniauth for users to sign in via email, twitter, and facebook. I'm not trying to allow users to tweet a message from inside the app.
I’ve got it currently working with the following code but it’s only posting from MY twitter account. I’m assuming this has to do with not setting up the Oauth_token correctly. No matter what account logins into the app, it still comes from my account.
In my User model, I have the following code (I’ve changed my key and tokens)…
def self.find_for_twitter_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
if user
return user
else
registered_user = User.where(:email => auth.uid + "#twitter.com").first
if registered_user
return registered_user
else
user = User.create(full_name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.uid+"#twitter.com",
oauth_token:auth.credentials.token,
oauth_secret:auth.credentials.secret,
password:Devise.friendly_token[0,20],
)
end
end
end
def tweet(tweet)
client = Twitter::REST::Client.new do |config|
config.consumer_key = "XXXXXXXX"
config.consumer_secret = "XXXXXXX"
config.access_token = "XXXXXXX-XXXXX"
config.access_token_secret = "XXXXXXX"
end
client.update(tweet)
end
In my config/initializer/devise.rb I have the following:
# Add Twitter OmniAuth
require 'omniauth-twitter'
config.omniauth :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
# Add Facebook OmniAuth
require 'omniauth-facebook'
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], :scope => 'basic_info, email, publish_stream'
In my view, I'm using a form for them to fill out and submit the tweet.
<p>
<%= form_for :tweet, url: tweets_path, method: :post do |f| %>
<%= f.text_field :message %>
<%= f.submit "Send Tweet" %>
<% end %>
</p>
You should authorize each twitter user with your app (as I remember with consumer key and consumer secret only).
I did this with simple way:
consumer = OAuth::Consumer.new($TWITTER_CONSUMER_KEY, $TWITTER_CONSUMER_SECRET, :site => "https://api.twitter.com")
request_token = consumer.get_request_token(:oauth_callback => "http://localhost/twitter/auth_callback")
return request_token.authorize_url
by URL I have
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier] )
token = access_token.token
secret = access_token.secret
Now if you set these toekn and secret in to your REST client you can post tweets from your user account.

Rails 3 getting gmail contacts using omniauth?

I am successfully login with google credentials using omniauth? omniauth is providing uid as following link
https://www.google.com/accounts/o8/id?id=xxxxxxxxxx
by using the above link is possible to get gmail contacts or their any other way to get gmail contact
No, Omniauth just provides authentication.
There is a gem that might be interesting for you: https://github.com/cardmagic/contacts
Quote: "Contacts is a universal interface to grab contact list information from various providers including Hotmail, AOL, Gmail, Plaxo and Yahoo."
Edit: Take a look at this blog post too: http://rtdptech.com/2010/12/importing-gmail-contacts-list-to-rails-application/
Get your client_id and client_secret from here. This is rough script, which works perfectly fine. Modified it as per your needs.
require 'net/http'
require 'net/https'
require 'uri'
require 'rexml/document'
class ImportController < ApplicationController
def authenticate
#title = "Google Authetication"
client_id = "xxxxxxxxxxxxxx.apps.googleusercontent.com"
google_root_url = "https://accounts.google.com/o/oauth2/auth?state=profile&redirect_uri="+googleauth_url+"&response_type=code&client_id="+client_id.to_s+"&approval_prompt=force&scope=https://www.google.com/m8/feeds/"
redirect_to google_root_url
end
def authorise
begin
#title = "Google Authetication"
token = params[:code]
client_id = "xxxxxxxxxxxxxx.apps.googleusercontent.com"
client_secret = "xxxxxxxxxxxxxx"
uri = URI('https://accounts.google.com/o/oauth2/token')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data('code' => token, 'client_id' => client_id, 'client_secret' => client_secret, 'redirect_uri' => googleauth_url, 'grant_type' => 'authorization_code')
request.content_type = 'application/x-www-form-urlencoded'
response = http.request(request)
response.code
access_keys = ActiveSupport::JSON.decode(response.body)
uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token="+access_keys['access_token'].to_s+"&max-results=50000&alt=json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
contacts = ActiveSupport::JSON.decode(response.body)
contacts['feed']['entry'].each_with_index do |contact,index|
name = contact['title']['$t']
contact['gd$email'].to_a.each do |email|
email_address = email['address']
Invite.create(:full_name => name, :email => email_address, :invite_source => "Gmail", :user_id => current_user.id) # for testing i m pushing it into database..
end
end
rescue Exception => ex
ex.message
end
redirect_to root_path , :notice => "Invite or follow your Google contacts."
end
end
Screenshot for settings.

Resources