Upgrade to Twitter4R v0.5.0 for OAuth goodness.

21 views
Skip to first unread message

SP

unread,
Aug 29, 2010, 2:37:27 AM8/29/10
to twitter4r-users
You will need to change a few things. Namely the way you create the
Twitter::Client instance.

Something like this would work:

client = Twitter::Client.new(:oauth_consumer => { :key => "your
key", :secret => "your secret" },
:oauth_access => { :key => "the key", :secret => "the
secret" }, :login => "the_users_screen_name")

Then you should be good to go. You can use the following script
inside the Twitter4R v0.5.0 gem to get you your access token details
for a test/developer user to play with:

$ T4R_GEMDIR/bin/t4r-oauth-access --key=consumer_key_here --
secret=consumer_secret_here

You will be guided through a 5 step process by the script.

HTH,
Susan
PS Twitter.com is turning off HTTP Basic support at the end of day
Tuesday August 31, 2010.

Andrew

unread,
Aug 31, 2010, 5:20:29 PM8/31/10
to twitter4r-users
I have a simple application that polls a twitter account for updates
and then posts them on a web site.

I have done the following:

1) upgrade twitter4r gem to 0.5.0 (with dependencies)
2) Created my application at twitter.com to get my consumer key/secret
pair
3) Updated my TwitterClient initialization to the example you posted
above

When the polling action attempts to get updated statuses from my
twitter account (which has approved access for my app) I get a 401
unauthorized response from the Twitter REST API. I can't figure out
what is going on here. Is there a problem with the app owner being the
same account as the account that is being polled by the app?

Any help or guidance would be appreciated.

Thanks,
Andrew

Andrew

unread,
Aug 31, 2010, 5:23:37 PM8/31/10
to twitter4r-users
I forgot to mention that I also obtained the access key/secret pair
via the t4r-oauth-access script provided with the gem.

SP

unread,
Aug 31, 2010, 5:26:59 PM8/31/10
to twitter4r-users
If you create the following YAML file:

test:
oauth_consumer:
key: <YOUR CONSUMER KEY/TOKEN>
secret: <YOUR CONSUMER SECRET>
oauth_access:
key: <YOUR ACCESS KEY/TOKEN>
secret: <YOUR ACCESS SECRET>

Then run the t4rsh script included in the v0.5.0 gem like so:
T4R_CONFIG=/path/to/your/yaml/file T4R_ENV=test /path/to/t4rsh

Can you do the following inside the shell:
tweets = @twitter.timeline_for(:friends)

??

If not can you make your app as simple as possible to post (without
the keys/secrets as I can use my own for both consumer and access
purposes to test).

Let me know.
Thanks,
Susan


On Aug 31, 4:20 pm, Andrew <ryoo...@gmail.com> wrote:

Darius Roberts

unread,
Aug 31, 2010, 5:33:57 PM8/31/10
to twitter...@googlegroups.com
I think you might already be logged in with the client? i seem to
remember that being an idiosyncracy of their api, with red herring
error messaging

D

> --
> You received this message because you are subscribed to the Google
> Groups "twitter4r-users" group.
> To post to this group, send email to twitter...@googlegroups.com.
> To unsubscribe from this group, send email to twitter4r-use...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/twitter4r-users?hl=en
> .
>

Andrew

unread,
Aug 31, 2010, 5:57:06 PM8/31/10
to twitter4r-users
Well the t4r shell returns results properly. My app essentially makes
one API call to twitter:

@twitter.timeline_for( :friends, :since => since )
# where since = the date of the last status update polled

So the t4rsh example you gave effectively simulates what my app needs
to do. I assumed that since the shell worked and my app didn't that I
had a problem with the configuration in my app. I switched from the
block syntax for the client configuration (Twitter::Client.configure)
to the file based syntax (Twitter::Client.from_config) using the yml
file I created for t4rsh and that seemed to solve the problem.

The original configure block I was using looked like:

Twitter::Client.configure do |conf|
conf.application_name = 'My App Name'
conf.application_version = ''
conf.application_url = 'My App URL'
conf.oauth_consumer_token = TWITTER_CONSUMER_KEY
conf.oauth_consumer_secret = TWITTER_CONSUMER_SECRET
end

I used the code at (http://github.com/twitter4r/twitter4r-core/blob/
master/examples/configure.rb) as an example. Perhaps that is an old/
incompatible example.

I seem to be up and running now. Thanks for the help.

Susan Potter

unread,
Aug 31, 2010, 6:00:07 PM8/31/10
to twitter...@googlegroups.com
Try to construct your Twitter::Client with the following hash:
:oauth_access => { :key => "your key", :secret => "your secret" }

i.e. you should initialize this way:

client = Twitter::Client.new(:oauth_access => { :key => "your key",
:secret => "your secret" })
tweets = client.timeline_for(:friends, :since => since)

HTH and reply by even if it works just to reference and to potentially
help someone else if they search the mailing list.
Thanks,
Susan

> --
> You received this message because you are subscribed to the Google Groups "twitter4r-users" group.
> To post to this group, send email to twitter...@googlegroups.com.
> To unsubscribe from this group, send email to twitter4r-use...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/twitter4r-users?hl=en.
>
>

--
mailto:m...@susanpotter.net
twitter:@SusanPotter
github:mbbx6spp
blog:http://geek.susanpotter.net
linkedin:http://www.linkedin.com/in/susanpotter

Andrew

unread,
Aug 31, 2010, 6:18:40 PM8/31/10
to twitter4r-users
So if I use the folowing:

client = Twitter::Client.new( :oauth_access => { :key =>
'myKey', :secret => 'mySecret } )
tweets = client.timeline_for(:friends, :since => since)

I get the following stack trace:

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:53:in `rest_consumer'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:62:in `rest_access_token'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:16:in `rest_oauth_connect'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/timeline.rb:78:in `timeline_for'
from (irb):3

I was assuming this was because I did not have the consumer key/secret
pair in the Client initializer. If I add that pair in as well I get a
401 unauthorized error (see below).

client = Twitter::Client.new( :oauth_access => { :key =>
'myKey', :secret => 'mySecret }, :oauth_consumer => { :key =>
'myConsumerKey', :secret => 'myConsumerSecret' } )
tweets = client.timeline_for(:friends, :since => since)

Twitter::RESTError: HTTP 401: Unauthorized at
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:92:in `raise_rest_error'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:97:in `handle_rest_response'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/base.rb:23:in `rest_oauth_connect'
from /Users/alee/.rvm/gems/ruby-1.8.7-p174/gems/twitter4r-0.5.0/lib/
twitter/client/timeline.rb:78:in `timeline_for'
from (irb):4
> > For more options, visit this group athttp://groups.google.com/group/twitter4r-users?hl=en.

Susan Potter

unread,
Sep 1, 2010, 1:23:25 PM9/1/10
to twitter...@googlegroups.com
Andrew,

I have been unable to reproduce the 401 except when my oauth
credentials are incorrect. I have found another bug that will affect
you though. Namely that the :since parameter is not going to be
passed into the query string to the Twitter.com API. I will be
working on a fix this afternoon and will try to release 0.5.1 bugfix
release including this bugfix and one other.

As a workaround since the
Twitter::Client.from_config('relative/path/to/twitter.yml', 'test')
worked you can use that YAML file that worked in the t4rsh to write
your script perhaps until I can reproduce the problem using correct
credentials.

HTH,
Susan

> For more options, visit this group at http://groups.google.com/group/twitter4r-users?hl=en.

Andrew

unread,
Sep 1, 2010, 1:27:18 PM9/1/10
to twitter4r-users
The from_config workaround will work fine for us. The bug fix will be
nice as it will cut down on unnecessary traffic and processing. Thanks
for looking into both issues.

scottwb

unread,
Sep 2, 2010, 6:30:24 AM9/2/10
to twitter4r-users
Where do I find this:

> $ T4R_GEMDIR/bin/t4r-oauth-access --key=consumer_key_here --
> secret=consumer_secret_here

I installed v0.5.0 with `gem install twitter4r` but I don't see that
there is a bin directory at all: http://img.skitch.com/20100902-8hmai1uhgcda3d23myb16usn88.png

Thanks,
-Scott

Andrew

unread,
Sep 2, 2010, 10:20:30 AM9/2/10
to twitter4r-users
I had the same problem and just downloaded the t4r-oauth-access and
t4rsh files from github. I modified the t4r-oauth-access file slightly
since it did not seem to pick up my ENV variables properly.

David

unread,
Sep 2, 2010, 1:15:58 PM9/2/10
to twitter4r-users

I can't believe I start playing with the Twitter API today right after
their API change :) I've tried all the various methods in this post
but I'm getting this error right now. Is there a different version of
the oauth gem I should be using? I'm using 0.5.0 of twitter4r.

.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/signature/hmac/
base.rb:12:in `digest'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/signature/base.rb:
65:in `signature'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/signature.rb:23:in
`sign'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/client/helper.rb:
45:in `signature'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/client/helper.rb:
75:in `header'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/client/net_http.rb:
90:in `set_oauth_header'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/client/net_http.rb:
30:in `oauth!'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/consumer.rb:225:in
`sign!'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/consumer.rb:191:in
`create_signed_request'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/consumer.rb:162:in
`request'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/tokens/
consumer_token.rb:25:in `request'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/tokens/
access_token.rb:11:in `request'
.rvm/gems/jruby-1.5.1/gems/oauth-0.4.3/lib/oauth/tokens/
access_token.rb:24:in `get'
.rvm/gems/jruby-1.5.1/gems/twitter4r-0.5.0/lib/twitter/client/base.rb:
19:in `rest_oauth_connect'
.rvm/gems/jruby-1.5.1/gems/twitter4r-0.5.0/lib/twitter/client/
timeline.rb:78:in `timeline_for'

Andrew

unread,
Sep 2, 2010, 3:18:10 PM9/2/10
to twitter4r-users
@David
Have you tried using from ruby 1.8.7 instead of jruby? I don't know
that the oauth or twitter4r gems are fully compatible with jruby.
Perhaps Susan could speak to the jruby compatibility for twitter4r? I
also noticed that you are using the 0.4.3 version of the oauth gem.
That's different than my setup (see below):

My working setup uses the following:
OS X (snow leopard)
rvm 0.1.38
ruby 1.8.7-p174
twitter4r 0.5.0
oauth 0.4.2

YMMV

SP

unread,
Sep 2, 2010, 4:30:24 PM9/2/10
to twitter4r-users
Hi all,

I have been trying to determine a good set of procedures to establish
if/when the OAuth credentials are incorrect or being passed
incorrectly to the Twitter::Client since I am unable to reproduce with
my credentials (unless I make a mistake in my OAuth credentials).

To remove both Twitter4R out of the dependency loop I am going to step
through the process of verifying you haven't copied and pasted or fat-
fingered the key/secrets (the pairing to be called tokens from here
on) at all or mixed up the consumer with the access tokens:
1. Install the temporary (but fixed) version of twurl gem named
mbbx6spp-twurl: [sudo] gem install mbbx6spp-twurl
2. Run twurl authorize subcommand like below to authorize your
consumer key/secret for a specific user:
$ twurl authorize -c <consumer_key> -s <consumer_secret> -u
<screen_name> -p <password>
3. It will have you do something just like t4r-oauth-access script.
4. Look at your ~/.twurlrc file contents. It should look something
like this:
---
profiles:
<screen_name>:
<consumer_key>:
username: <screen_name>
consumer_key: <consumer_key>
consumer_secret: <consumer_secret>
token: <access_key>
secret: <access_secret>
configuration:
default_profile:
- <screen_name>
- <consumer_key>

Use these values in your twitter.yml file and use
Twitter::Client#from_config method to load the credentials. Of course
you need to make some translations namely:
username => login
consumer_key => key (inside the oauth_consumer hash)
consumer_secret => secret (inside the oauth_consumer hash)
token => key (inside the oauth_access hash)
secret => secret (inside the oauth_access hash)

I am still working on a 0.5.1 release with two bugfixes. As soon as I
have wrapped up enough of my paid work for the week I will be able to
release it. ETA Friday (tomorrow) evening US central daylight savings
timezone (GMT-5h).

HTH,
Susan

On Sep 2, 12:15 pm, David <david.g.y...@gmail.com> wrote:

SP

unread,
Sep 2, 2010, 4:38:41 PM9/2/10
to twitter4r-users
@Andrew,

Thanks for noticing those differences.

@David,

I have not tested Twitter4R on JRuby specifically. I have tested on
the following MRI implementations:
* 1.8.6p399
* 1.9.2.0
And the following REE implementations:
* 1.8.7-2010.01

HTH,
Susan

Soyapi Mumba

unread,
Sep 3, 2010, 9:19:56 PM9/3/10
to twitter4r-users
Susan et al

I've managed to get both Twitter::Client#from_config and
Twitter::Client.new approaches working from the authorised domain.

To fix the Twitter::Client.new approach, I changed all :key and
:secret symbols to strings. If you compare the Twitter::Client objects
created by the two apporaches, you will notice that the only
significant difference is the fact that Twitter::Client.new uses
symbols for key and secret hash keys while the other one uses strings.

The snippet below works for me using Twitter::Client.new

client = Twitter::Client.new(
:oauth_consumer => { 'key' => CONSUMER_KEY,
'secret' => CONSUMER_SECRET },
:oauth_access => { 'key' => ACCESS_KEY,
'secret' => ACCESS_SECRET },
:login => "owinna")
client.messages(:received)

Hope that helps.

- Soyapi

Steve

unread,
Sep 3, 2010, 9:57:33 PM9/3/10
to twitter4r-users
SWEET that totally solves my problem too! Thanks so much...

Steve

JS

unread,
Sep 3, 2010, 6:12:48 PM9/3/10
to twitter4r-users
I had the same problem as Andrew.

I simply used strings in the hash like the Twitter::Client.from_config
method does and got rid of the authentication pb:

client = Twitter::Client.new(
'oauth_access' => { 'key' => 'twitter_user_token', 'secret' =>
'twitter_user_secret' },
'oauth_consumer' => { 'key' => 'consumer_token', 'secret' =>
'consumer_secret' }
)
client.status( :post, 'Hello again Twitter.' )

Enjoy.

SP

unread,
Sep 4, 2010, 10:17:56 AM9/4/10
to twitter4r-users
@JS: Goodness, thank you for solving my mystery. I was not thinking
and the issue was bugging me. I will resolve this in 0.5.1, which I
didn't get around to working on yesterday.

SP

unread,
Sep 4, 2010, 10:26:47 AM9/4/10
to twitter4r-users
That should have been @Soyapi, but thanks to everyone to helping out
with this. Just goes to show how being too close to the problem can
cloud your mind.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages