The documentation says
config = Twitter.configure do |config|
config.consumer_key = 'YOUR_CONSUMER_KEY'
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
config.oauth_token = 'YOUR_OAUTH_TOKEN'
config.oauth_token_secret = 'YOUR_OAUTH_TOKEN_SECRET'
end
and
client.update('Hello, from Twitter Gem!')
But where do I put them? (also, why do documentation assume everyone knows where to put things?)
What is one valid design for doing this
For example, I have a twitter button on a post. When the twitter button is clicked, I want it to tweet that post (nevermind about handling shortening the post).
Should i put the tweeting client.update('Hello, from Twitter Gem!') in an action of a newly created twitter controller?
Or make it a button that calls a javascript file with that tweeting code in it?
I just need one valid way of getting the tweeting button to be functional.
I have used the following setup:
config/initializers/twitter.rb
$twitter = Twitter.configure do |config|
config.consumer_key = 'YOUR_CONSUMER_KEY'
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
config.oauth_token = 'YOUR_OAUTH_TOKEN'
config.oauth_token_secret = 'YOUR_OAUTH_TOKEN_SECRET'
end
Then you can reference the $twitter variable in a controller of your choosing. Your view can post a message to the controller, you can do any preprocessing (if needed), and then call $twitter.update(message)
I like this setup because it allows me to access my configured twitter client from wherever I need it.
Although the question was about file paths, and #danielM is correct, the code is outdated. This was the config setup method youll need to call: Error with Ruby Twitter API
Related
I ideally want to access the API from this website, but since I am struggling to do that, I have decided to try and scrape the page instead. I am starting at this page:
https://fantasy.sixnationsrugby.com/#/welcome/login
Where I plan to log in and then scrape the data.
The code I have below seems to work for every other website I test with, apart from this one. And I can't seem to pull anything, no text, forms, etc literally nothing works? As an example I just want to scrape the main header title 'Let's Go! Log in to your account'
def scrape
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get('https://fantasy.sixnationsrugby.com/#/welcome/login')
header_title = page.search('div.fs-box-header-title').text.strip
#output = header_title
end
Is it something to do with how the page is rendered? Thanks
I am trying to use the google-ads-ruby library to allow our ruby on rails application users to connect our app with Google Ads and pull some stats from their account for them.
I installed the gem and managed to authenticate a user and get the refresh_token.
Now I'm trying to start collecting data from Google.
The first thing that fails is their instructions to require the gem in my code with require 'google/ads/google_ads'
I tried adding it to my controller and got cannot load such file -- google/ads/google_ads
Then, according to their instructions, I should be able to run this:
client = Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
config.client_id = Rails.application.secrets.google_oauth_client_id
config.client_secret = Rails.application.secrets.google_oauth_client_secret
config.developer_token = Rails.application.secrets.google_developer_token
config.refresh_token = #user.google_ads.refresh_token
end
accessible_customers = client.service.customer.list_accessible_customers().resource_names
accessible_customers.each do |resource_name|
puts "Customer resource name: #{resource_name}"
end
and then list, for example, the user's accounts, as described here.
However, I am getting uninitialized constant Google::Ads::GoogleAds
Does anyone know what is going on?
Have you tried?
client = ::Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
config.client_id = Rails.application.secrets.google_oauth_client_id
config.client_secret = Rails.application.secrets.google_oauth_client_secret
config.developer_token = Rails.application.secrets.google_developer_token
config.refresh_token = #user.google_ads.refresh_token
end
This is not really an answer to my question. I was unable to find a solution, but doing some more digging, I found the AdsWords on Rails example app Google added to that same gem and the documentation
The app is slightly outdated and you will probably hate getting it to work. Also, it's written in a very cryptic way and includes so many functions just to use their API... but I was able to make it work. To be honest, someone should write a tutorial.
Hope this may give some clues to someone who's lost at some point.
I'm creating a really simple Rails application with one specific purpose: add a Calendar event to a Google Calendar account.
I'm following the Ruby Guide from the Google Calendar API.
I was able to run the provided testing code (Ruby only, no framework) but I'm having a hard time accessing the credentials from a Rails project and I'm not sure the proper ("idiomatic"?) way to do it and organize the project.
Part of the process is using OAuth 2.0 since this goal requires access to Google User's private data (both read and write), I'm following the Using OAuth 2.0 for Web Services instructions.
Right now I have several different questions regarding best practices and/or the proper way to organize code:
Google provides a client_secret.json that have the credentials to access the application. Where should I keep it? Should I keep it in a .env file in the Development environment and (in my case) in Heroku's ENV VARS in the Production Environment?
I tried keeping the client_secret.json file in the project's root folder (same path as the Gemfile), added it to the .gitignore but I wasn't able to require "#{Rails.root}/client_secret.json":
/Users/jsoifer/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require': No such file to load -- /Users/jsoifer/Developer/Tiny-Things/tiny-booking/client_secret.json (LoadError)
I created a services/ folder to put the Google Calendar related code inside, I wasn't sure if I should put is in a controller though. How should I organize this?
Important consideration:
I'm not using any other method of Authentication/Authorization such as Devise or others and I'm not planning to do so right now. I just want to get Google's Authorization token and create a Calendar event.
Github Project Link
I was able to figure this out and will post the answer to each of the questions below:
One of the possible locations for the client_secret.json file is config/client_secret.json.
When shipping to Production in Heroku, use ENV Vars.
Require is not the appropriate way to import the credentials in the json file.
Use Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) ) (assuming the file is indeed in config/.
There are several different alternatives as on how to organize code. I ended up creating a services folder and a google_calendar.rb class holding the authorization logic.
Here's the code:
app/services/google_calendar.rb
require 'google/api_client/client_secrets'
require 'google/apis/calendar_v3'
class GoogleCalendar
# Attributes Accessors (attr_writer + attr_reader)
attr_accessor :auth_client, :auth_uri, :code
def initialize
# ENV: Development
# Google's API Credentials are in ~/config/client_secret.json
client_secrets = Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) )
#auth_client = client_secrets.to_authorization
# Specify privileges and callback URL
#auth_client.update!(
:scope => 'https://www.googleapis.com/auth/calendar',
:redirect_uri => 'http://localhost:3000/oauth2callback'
)
# Build up the Redirecting URL
#auth_uri = #auth_client.authorization_uri.to_s
end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
# Starting action in config/routes.rb
def welcome
# Redirect to Google Authorization Page
redirect_to GoogleCalendar.new.auth_uri
end
def token
# Get a auth_client object from Google API
#google_api = GoogleCalendar.new
#google_api.auth_client.code = params[:code] if params[:code]
response = #google_api.auth_client.fetch_access_token!
session[:access_token] = response['access_token']
# Whichever Controller/Action needed to handle what comes next
redirect_to new_event_path()
end
end
I've been using the gem LinkedIn OAuth 2.0. Right now I can get it to generate the linkedin signin page. However, the next thing that is supposed to happen is it sends to my callback link a code which I use to generate an access token. The problem is that the variable 'oauth' is generated in the authenticate action but then needs to be used again in the callback action. I've tried generating the oauth variable again using the exact same parameters, but when I do that I get an SSL certificate error. It seems like the exact same oauth instance needs to be used in both cases. Let me know if you have any thoughts. My code is below:
def authenticate
require "linkedin-oauth2"
LinkedIn.configure do |config|
config.client_id = "Mycode"
config.client_secret = "Mysecret"
# This must exactly match the redirect URI you set on your application's
# settings page. If your redirect_uri is dynamic, pass it into
# `auth_code_url` instead.
config.redirect_uri = "http://localhost:3000/auth/linkedin/callback"
end
oauth = LinkedIn::OAuth2.new()
url = oauth.auth_code_url
redirect_to url
end
def callback
require "linkedin-oauth2"
code = params[:code]
access_token = oauth.get_access_token(code)
api = LinkedIn::API.new(access_token)
my_job_titles = api.profile(fields: ["id", {"positions" => ["title"]}])
puts my_job_titles
redirect_to("/")
end
end
Getting an SSL certificate error doesn't mean that the instantiation is wrong. I don't know that gem, but I can't see why would that be a problem.
The require and the configuration block should not be inside the method (maybe you forgot the configuration from the second method?); the best place for those is in config/initializers/linkedin_oauth2.rb.
If you don't want to load it at startup, then you can put those in a private method oauth with memoization:
def oauth
#oauth ||=
begin
require "linkedin-oauth2"
LinkedIn.configure do |config|
...
end
LinkedIn::OAuth2.new()
end
end
If the SSL error still occurs, you should investigate that. You can try creating a simple Ruby script with some example from the gem's readme, just to test the connection to LinkedIn.
Looks like the gem is using the faraday gem for HTTP, you can also try using that directly to make a simple call to LinkedIn.
Using OmniAuth, Rails 3.1.0.rc2, mysql2, ruby 1.9.2.p0.
I still get this when redirecting back to my site.
/auth/failure?message=invalid_response
Omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, 'XXXXX', 'XXXXXXXXXXXXX'
I've checked the keys 100x and they are correct. Still getting the invalid response. Any of the questions I see don't seem to work.
Any help would be greatly appreciated!
Thanks. [:
If any new information is needed, just ask.
If you use this request.env['rack.auth'] in your controller, change this to request.env['omniauth.auth'] - this were explained here OmniAuth
this solution works for me.
I had a similar problem. It turns out that I actually had some runtime errors in my Users::OmniauthCallbacksController#twitter method:
I was calling a method on a non-existent method on a nil object and this was raising an exception, but either devise or omniauth were swallowing the exception.
I ended up wrapping my entire method body in a begin/rescue clause and printing out the exception.
However, if you are getting Invalid Credentials then it's likely that the twitter-issued oauth key has expired and so your user should really be calling /users/auth/twitter again.
Are you sure you are not putting the keys in the wrong order?
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, TW_CONSUMER_KEY, TW_CONSUMER_SECRET
end
If not, let's quickly test your credentials with the Twitter gem:
# twitter.rb -- Test credentials
require "rubygems"
require "twitter"
# Get a user's most recent status update
puts Twitter.user_timeline("YOUR_USER").first.text
Twitter.configure do |config|
config.consumer_key = TW_CONSUMER_KEY
config.consumer_secret = TW_CONSUMER_SECRET
end
# Update your status
Twitter.update("I Love ruby!")
If it works, then your credentials are fine... you should keep looking into Rails...
Thank you Christian for your answer. It was very helpful for me. But if it gives a 401 error trying to update, retweet, etc you will have to include
config.oauth_token = 'MY_OAUTH_TOKEN'
config.oauth_token_secret = 'MY_OAUTH_TOKEN_SECRET'
to Twitter client configuration. Look at https://dev.twitter.com/discussions/1522
So finally you will have
Twitter.configure do |config|
config.consumer_key = 'TW_CONSUMER_KEY'
config.consumer_secret = 'TW_CONSUMER_SECRET'
config.oauth_token = 'MY_OAUTH_TOKEN'
config.oauth_token_secret = 'MY_OAUTH_TOKEN_SECRET'
end
It worked for me
and of course your Twitter app has to have Access level = Read and write. You have to change this in dev.twitter.com if you want to update the status, retweet, etc
Have you tried omniauth-twitter gem?? https://github.com/arunagw/omniauth-twitter