#359 Twitter Integration pro
Jun 15, 2012 | 14 minutes | Plugins
Here I show how to integrate Twitter into a Rails app. This includes fetching data, caching it, and even how to authenticate as a user signing in to your application through Twitter.
- source codeProject Files in Zip (119 KB)
- mp4Full Size H.264 Video (32.4 MB)
- m4vSmaller H.264 Video (18.2 MB)
- webmFull Size VP8 Video (13.3 MB)
- ogvFull Size Theora Video (44.4 MB)
Great value as usual, just one question Ryan.
Do you dislike
find_or_create(which I could understand BTW) or
maybe think that
exists? ... || create!has other merits (like the bangs exception raising behavior).
I'd been wrestling with trying to cache my 'latest tweet' partial and the cron job not expiring it properly for a while, this is a much tidier way to do it, with no delay in page loads.
Just one question: I saw in the end that it's possible to change app permission dinamically, is it possible to change app name too? It would be useful for I18n, I have an app that has a name associated to an idiom and another name for another idiom.
cachecan take an array, so you should do
cache [testimonies, Date.today], no?
I've actually been having troubles storing the tweets in the database. With using postgresql, I get this error:
: SELECT 1 AS one FROM "tweets" WHERE "tweets"."tweet_id" = 222042501954019328 LIMIT 1
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = bigint
I had to actually declare the table creation differently.
per this article: http://moeffju.net/blog/using-bigint-columns-in-rails-migrations
Not sure if this is the best method - but currently working on my end!
This was very helpful for me. Thanks for sharing.
Thanks for posting, very useful.
Although you shouldn't need the id => false in the create_table declaration. The example given from Matt Bauer from what I can see is using the id as the bigint column whereas here we have replaced it with tweet_id. I have tested this myself and I can confirm it works.
Super helpful. Thanks!
Can someone fill in the gap that occurs around 8:40? I'm using the latest version of the Twitter gem, and when I set up a twitter.rb initializer as specified, and configure environment variables to match up with the lines in the twitter.rb, go into the rails console, and run Twitter.favorites(count: 1) to test things, I get:
Twitter::Error::Unauthorized: This method required authentication...
Did something change in the Gem? Or, are there steps missing from the railscast?
I've basically followed the directions exactly, with the exception that the Gem I use is the latest Twitter Gem, located at github.com/sferik/twitter.
Also having this problem...Unsure as to how to treat that initializer file.
Did you find the solution of this issue?
I'm having this same issue. Have these steps changed with the new Twitter API updates?
Please refer to the new changes in sferik's gem: https://github.com/sferik/twitter#configuration
Is anyone having a problem displaying the profile_image_url? My code is below. The only thing that will display is the alt text of the img, never the image itself. However, when I review HTML, it has a url in the img and if I hover over the link it displays the image itself. This tells me the path is correct.
Any suggestions would be greatly appreciated.
The profile image is tweet.user.profile_image_url
I may be confused on the Twitter Secret & Consumer keys... but should these values be stored in a hash?
In the unfortunate event of a database being hacked - wouldn't that give access to post on user's Twitter accounts?
The idea of the keys is to limit the access only to the API. So in that case you can still log in to Twitter and reset your application keys.
Don't make the mistake of setting tweet_id's type to an integer. MySQL maxes out at 2147483647 unless you specify a limit. It's easier to just make it a string.
How is it possible to extract hashtags via the twitter gem? I tried with:
@hashtags ||= entities(Twitter::Entity::Hashtag, :hashtags)
But it is on the view site I get in trouble! :s
How do I do the same for a authorized user, I've got the following in my controller, model, and index;
As you've guessed it, I'm trying to use a refresh button to pull the route /refresh which runs the controller, that runs the model??
Not sure I'm doing this right. However, going in the console with the following command;
I get an error: NameError: uninitialized constant User::Newtweet
Should I be moving the self.pull_tweets stuff into a controller??
Any suggestions, thank you!
If you want to use Twitter authentication in https://teatro.io/ first create special stage application in Twitter and then set domain as project-token.ttrcloud.com and authentication urls as http://project-token.ttrcloud.com. It will work on any branch of this project.
If at 2:45 when Ryan interacts with Twitter methods in the console you receive an "Unable to verify your credentials" error, see this Stack Overflow thread:
Steps involve copying and pasting the Twitter.configure block mentioned in the thread into omniauth.rb, then supplying config.oauth_token and config.oauth_token_secret with values from dev.twitter.com.
This is done by generating access tokens. I did this by visiting the Twitter app I created, clicking on the "API Keys" tab, then generating the access tokens at the bottom of the page. Doing so will supply you with values that you can plug into the Twitter.configure block mentioned above.
One thing that isn't clear is this: Does a User have to authenticate with Twitter (through, like OmniAuth), or can they just authorize an app with read | read/write access?