extracting tweets without oauth - ruby

I have tried various ruby codes to extract tweets, i came to know that in order to get tweets i have to create a twitter application and use Oauth for getting consumer key. My question here is can i not access the public tweets without using Oauth since i am working on a project where i have to analyze tweets.
The ruby code that i tried is as follows
require 'tweetstream'
require 'rubygems'
TweetStream.configure do |config|
config.username = 'twitterusername'
config.password = 'twitterpassword'
config.auth_method = :basic
end
#client = TweetStream::Client.new
#client.sample do |status|
puts "#{status.text}"
end
I get Failed to reconnect to twitter error.

There is no access to the twitter api with out Oauth. You will need to create a twitter application in order to request this information from the API.
You can start reading here: Authentication & Authorization

Related

Twitter get_request_token returns 403 Forbiddne

I have not been able to get an oauth request token using on Twitter API.
I am following the code sample provided by Twitter here:
I am using the following gems:
ruby '2.7.6'
gem 'typhoeus'
gem 'oauth'
I have checked on the developer portal on Twitter that I have set the Twitter Developer Portal User Authentication Settings and have verified that I have both OAuth 2.0 API v2 only and OAuth 1.0a API v1.1 and v2 enabled and I have set both read and write access
Under the whitelisted callback urls I have specified: http://127.0.0.1:8000 and http://127.0.0.1:3000
In the code I have the following:
require 'oauth'
require 'json'
require 'typhoeus'
require 'oauth/request_proxy/typhoeus_request'
consumer_key = ENV["CONSUMER_KEY"]
consumer_secret = ENV["CONSUMER_SECRET"]
create_tweet_url = "https://api.twitter.com/2/tweets"
#json_payload = {"text": "Hello world!"}
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
:site => 'https://api.twitter.com',
:authorize_path => '/oauth/authenticate',
:debug_output => true)
def get_request_token(consumer)
request_token = consumer.get_request_token(oauth_callback: 'http://127.0.0.1:8000/oauth/callback')
return request_token
end
but this fails every time with token_request: 403 Forbidden
I have verified and reverified that I am setting the correct Environment variables via export CONSUMER_KEY=XXXXXXXXXXXXXXX etc
I have also verified that the ruby script is picking up those environment variables.
I have also looked at the POST request to see if I can see anything there that looks wrong but I cant see anything.
"POST /oauth/request_token HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: OAuth gem v0.5.10\r\nContent-Length: 0\r\nAuthorization: OAuth oauth_callback=\"http%3A%2F%2F127.0.0.1%3A8000%2Foauth%2Fcallback\", oauth_consumer_key=\"XXXOAUTH_CONSUMER_KEY_HIDDENXXXX\", oauth_nonce=\"keyXXXXXXXXHIDINGTHISKEYTOOXXXXXX\", oauth_signature=\"UgkOIhqtZ1ZEndyqTC173LwEUTo%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1658409008\", oauth_version=\"1.0\"\r\nConnection: close\r\nHost: api.twitter.com\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
I don't know where to look next.
What I don't see in the POST request is the SECRET Being sent.
So I tried to hard code the secret into the OAuth::Consumer.new('APP_KEY_HERE', 'SECRET_KEY_HERE') but no joy

Google adwords api how to authorize with service account impersonation

I'm trying to develop a ruby script to query the Google Adwords API using service account credentials. I know that the json credentials file works in another script which isn't ruby but I can't get past authorization in this ruby script. I can't find any examples using this pattern. I don't want to use OAuth2. I know this script is not well developed but I'm just trying to get the basics. What am I doing wrong here?
My company has a G Suite account and I have full administrator permissions.
Here is my code:
#!/usr/bin/ruby
require 'googleauth'
require 'fileutils'
require 'adwords_api'
API_VERSION = :v201809
scopes = ["https://www.googleapis.com/auth/adwords"]
credential_file = File.open('credentials/adwords-production.json')
prn = "adwords#mycompany.com"
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: credential_file, scope: scopes, prn: prn)
def get_report_fields(report_type)
adwords = AdwordsApi::Api.new
report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)
# Get report fields.
fields = report_def_srv.get_report_fields(report_type)
if fields
puts "Report type '%s' contains the following fields:" % report_type
fields.each do |field|
puts ' - %s (%s)' % [field[:field_name], field[:field_type]]
puts ' := [%s]' % field[:enum_values].join(', ') if field[:enum_values]
end
end
end
begin
report_type = 'ACCOUNT_PERFORMANCE_REPORT'
get_report_fields(report_type)
end
From Google's docs here:
All AdWords API calls must be authorized through OAuth2. OAuth2 enables your AdWords API client app to access a user's Google Ads account without having to handle or store the user's login info.
It appears you have to use OAuth2 Authentication.
Further reading here confirms this:
Your app will need to access user data and contact other Google services on your behalf. Authentication via OAuth2 allows your app to operate on behalf of your account.
To enable your app to access the API, you need an OAuth2 client ID and client secret.
You say you have done this somewhere else where you were able to use a JSON file, so I looked into the source a little.
The source for the Ruby API is here and here. It appears there really is no other way to manage credentials, at least in the Ruby API. Looking here:
# Retrieve correct soap_header_handler.
#
# Args:
# - auth_handler: instance of an AdsCommon::Auth::BaseHandler subclass to
# handle authentication
# - version: intended API version
# - header_ns: header namespace
# - default_ns: default namespace
#
# Returns:
# - SOAP header handler
#
def soap_header_handler(auth_handler, version, header_ns, default_ns)
auth_method = #config.read('authentication.method', :OAUTH2)
handler_class = case auth_method
when :OAUTH2, :OAUTH2_SERVICE_ACCOUNT
AdsCommon::SavonHeaders::OAuthHeaderHandler
else
raise AdsCommon::Errors::AuthError,
"Unknown auth method: %s" % auth_method
end
return handler_class.new(#credential_handler, auth_handler, header_ns,
default_ns, version)
end
Notice that unless OAuth is used, it throws an error. There really is no other way to authenticate. Even if you managed to authenticate another way and try passing it to the credential manager, it will reject it.
Short answer: This is not possible in the Ruby API without hacking it some way.

Using ruby google-api-client to get youtube video data

Bonjour !
I have seen a lot of similar questions, but can't figure out why none of them brings a clear answer.
I'm trying to use the google-api-client gem to connect to youtube data api to retrieve a thumbnail image from a youtube video link.
I assume I don't need to deal with that (obscure to me) oAuth authentication.
First step : I create the project with google api console, authorizing the Youtube Data API. In the "API Access" tab, I select "Create a client ID..." with option "Service Account".
It gave me a client ID, email address and public key fingerprints.
Second : I create my ruby script using the google-api-client gem. I just copy/paste the example in the documentation that is :
require 'google/api_client'
client = Google::APIClient.new
From my client object I thought I could directly call
client.execute()
to get my atom feed with all my video information. But I just have an idea how to do it.
Maybe I also have to get oAuth authorization ? To get the token ?
What am I missing ?
Many thanks for your contributions.
That wasn't so hard, I found out how to do it.
You don't need to deal with oAuth authentication in that case. You will need to generate a server API Key and send it on each request.
The following snippet will help retrieving all information about a video the API provides.
require 'google/api_client'
require 'json'
client = Google::APIClient.new
youtube = client.discovered_api('youtube', 'v3')
client.authorization = nil
result = client.execute :key => "<YOUR_SERVER_API_KEY>", :api_method => youtube.videos.list, :parameters => {:id => '<YOUR_VIDEO_ID>', :part => 'snippet'}
result = JSON.parse(result.data.to_json)

Is it possible to get Gmail oauth or xauth tokens with OmniAuth?

I want to get oauth or xauth tokens from GMail to use with gmail-oauth. I'm thinking of using OmniAuth but it seems not to support GMail yet, which means that with stock OmniAuth is impossible. Is that correct? Am I missing something?
Omniauth has support for both OAuth and OAuth2, which will both allow you to authenticate a google account.
Here are all of the strategies you can use via omniauth:
https://github.com/intridea/omniauth/wiki/List-of-Strategies
Here are the two google OAuth gems:
omniauth-google (OAuth1)
omniauth-google-oauth2 (OAuth2)
As per the documentation of the first gem:
Add the middleware to a Rails app in config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google, CONSUMER_KEY, CONSUMER_SECRET
# plus any other strategies you would like to support
end
This is done in addition to setting up the main omniauth gem.
I had trouble, like you, using existing gems with OAuth2 and Gmail since Google's OAuth1 protocol is now deprecated and many gems have not yet updated to use their OAuth2 protocol. I was finally able to get it to work using Net::IMAP directly.
Here is a working example of fetching email from Google using the OAuth2 protocol. This example uses the mail, gmail_xoauth, omniauth, and omniauth-google-oauth2 gems.
You will also need to register your app in Google's API console in order to get your API tokens.
# in an initializer:
ENV['GOOGLE_KEY'] = 'yourkey'
ENV['GOOGLE_SECRET'] = 'yoursecret'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
scope: 'https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email'
}
end
# ...after handling login with OmniAuth...
# in your script
email = auth_hash[:info][:email]
access_token = auth_hash[:credentials][:token]
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', email, access_token)
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
mail = Mail.read_from_string msg
puts mail.subject
puts mail.text_part.body.to_s
puts mail.html_part.body.to_s
end

How to use Twitter gem in Rails?

I am planning to use the Twitter gem in my app, but I am stuck trying to figure out what an OAUTH token is, and where can get it. Twitter's documentation has been no help to me so far.
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
Twitter.user
Can anyone gently explain to me what I need to do in order to set this up correctly to use in my app?
You should only need the consumer key and consumer secret. The oauth token and oauth secret are for the non-browser version of the app (I can't remember the actual word) which allows you to have a permanent link to twitter from an account to tweet without authorizing each time.
Go to http://developer.twitter.com/
Register an app (make one up)..
All the authentication information you need will be provided.

Resources