bad authentication, twitter error badRequest on twitter API - ruby

I'm trying to use the twitter gem but I don't know why when I run my program I got this error.
I can "read and write and Direct message" with app permission on Twitter Developers.
This is my code:
require 'twitter'
require 'dotenv'
require 'pry'
Dotenv.load('.env')
def login_twitter
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
return client.update('My first tweet on ruby!')
end
login_twitter
And the error:
Traceback (most recent call last):
8: from app.rb:17:in `<main>'
7: from app.rb:14:in `login_twitter'
6: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/tweets.rb:128:in `update'
5: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/tweets.rb:157:in `update!'
4: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/utils.rb:64:in `perform_post_with_object'
3: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
2: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/utils.rb:50:in `perform_request'
1: from /Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/request.rb:41:in `perform'
/Users/user/.rvm/gems/ruby-2.7.2/gems/twitter-7.0.0/lib/twitter/rest/request.rb:97:in `fail_or_return_response_body': Bad Authentication data. (Twitter::Error::BadRequest)
I tried to regenerate my API keys and Acess Token, but the problem still occurs.

Related

Ruby AWS SDK error. Value nil in module_inheritable_attributes.rb

I have the following script:
require 'httparty'
require 'aws-sdk-s3'
include HTTParty
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('...', '...')
})
client = Aws::S3::Resource.new
movies = self.class.get("#{HOST}/movies")
When I run it I'm met with the following error:
Traceback (most recent call last):
10: from script.rb:33:in `<main>'
9: from script.rb:33:in `new'
8: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-athena-1.22.0/lib/aws-sdk-athena/resource.rb:14:in `initialize'
7: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/base.rb:99:in `new'
6: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-athena-1.22.0/lib/aws-sdk-athena/client.rb:262:in `initialize'
5: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/base.rb:19:in `initialize'
4: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/base.rb:62:in `build_config'
3: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/configuration.rb:149:in `build!'
2: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/configuration.rb:158:in `empty_struct'
1: from /Users/me/.rvm/gems/ruby-2.6.5/gems/aws-sdk-core-3.78.0/lib/seahorse/client/configuration.rb:158:in `new'
/Users/me/.rvm/gems/ruby-2.6.5/gems/httparty-0.17.1/lib/httparty/module_inheritable_attributes.rb:42:in `inherited': undefined method `each' for nil:NilClass (NoMethodError)
After going through AWS's documentation and tutorials I still can't find the issue. What could be wrong?
I found the issue to be the line include HTTParty. I declared the S3 client before that line and it works now:
require 'httparty'
require 'aws-sdk-s3'
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('...', '...')
})
client = Aws::S3::Resource.new
include HTTParty
movies = self.class.get("#{HOST}/movies")

Ruby's Twitter REST api not working

I am trying to use the ruby interface for twitter api.The Streaming Api is working but Rest spi is not.
Code :
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config|
# you must set up an application using Twitter's developer site, and set these values:
# (See the Configuration example)
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx-xxx"
config.access_token_secret = "xxx"
end
puts client.user('xyz')
I am always getting the following error -
Faraday::Builder is now Faraday::RackBuilder.
/home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:143:in `rescue in request': execution expired (Twitter::Error)
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:131:in `request'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/client.rb:97:in `get'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/api/utils.rb:118:in `object_from_response'
from /home/atul/.rvm/gems/ruby-2.1.0/gems/twitter-5.5.1/lib/twitter/rest/api/users.rb:257:in `user'
from actresses.rb:17:in `<main>'
I am trying to increase the time out because of this solution but am able to figure out how to do that.
I don't understand what am I doing wrong as the same code is working for Streaming api after making the appropriate changes.
I'm not entirely clear on what kind of an issue you're having, but to set the timeout you may want to try
custom_options = Twitter::Default::CONNECTION_OPTIONS.merge(
request: { open_timeout: 5, timeout: 20 } ) # default timeout is 10
client = Twitter::REST::Client.new do |config|
# you must set up an application using Twitter's developer site, and set these values:
# (See the Configuration example)
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx-xxx"
config.access_token_secret = "xxx"
config.connection_options = custom_options
end
I'm not sure if that will resolve that error, though.

`block in non_options': file not found: (ArgumentError)

I'm trying to open browser url based on argument passed to script. Hence I wrote following ruby code:
require 'selenium-webdriver'
require 'test/unit'
class TestTitle < Test::Unit::TestCase
def setup
$driver = Selenium::WebDriver.for :firefox
if ARGV[0] == 'google'
$driver.get 'http://www.google.com'
elsif ARGV[0] == 'twitter'
$driver.get 'http://www.twitter.com'
end
end
def test_title
puts $driver.title
end
def teardown
$driver.quit
end
end
When I passed argument: ruby test.rb 'google', it results into following error:
c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: google (ArgumentError)
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Please help me understand what I'm doing wrong.
It appears that test-unit (as of 1.9.1) grabs command line options in its GlobOptions module. You are using ARGV[0] to pass browser name, but it thinks you're passing a file name. A workaround is to capture the value of ARGV[0] and then clear it before your test case runs:
browser = ARGV[0]
ARGV[0] = nil

Get request to twitter API using Ruby, REST

I try to learn REST in Ruby using Twitter API.
According https://dev.twitter.com/docs/api/1/get/trends I have to write GET request to http://api.twitter.com/1/trends.json.
My Ruby code is:
require 'rubygems'
require 'rest-client'
require 'json'
url = 'http://api.twitter.com/1/trends.json'
response = RestClient.get(url)
puts response.body
But i'm getting next errors:
/home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient /abstract_response.rb:48:in `return!': 404 Resource Not Found (RestClient::ResourceNotFound)
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from /home/danik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /home/danik/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient.rb:68:in `get'
from TwitterTrends.rb:5:in `<main>'
What is wrong?
You are getting that error because the resource you are trying to fetch with http://api.twitter.com/1/trends.json does not exist, as is explained in this doc trends docs
This method is deprecated and has been replaced by GET trends/:woeid.
Please update your applications with the new endpoint.
You want to fetch a URL like this https://api.twitter.com/1/trends/1.json. So, in your code, try doing this:
require 'rubygems'
require 'rest-client'
require 'json'
url = 'https://api.twitter.com/1/trends/1.json'
response = RestClient.get(url)
puts response.body
And you should get a response.

need ignore basic http auth

I have trouble with getting page source.
require 'mechanize'
agent = Mechanize.new
page = agent.get("https://#{ip}/")
end have error:
/home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:682:in `response_authenticate': 401 => Net::HTTPUnauthorized for https://82.144.208.6/cgi-bin/welcome.cgi -- no credentials found, provide some with #add_auth -- available realms: r722 (Mechanize::UnauthorizedError)
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:288:in `fetch'
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize.rb:407:in `get'
from /home/lord/ruby/ruby_backup/backup-done.ru:35:in `block (2 levels) in <main>'
how can I ignore http auth, and get source? thx
The exception contains a page accessor. The documentation describes it: http://mechanize.rubyforge.org/Mechanize/ResponseCodeError.html
Try:
begin
page = agent.get ...
rescue Mechanize::ResponseCodeError => e
page = e.page
end

Resources