understanding twitter gem readme

I have few questions about this portion of the readme of the twitter gem

Client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"
end

After configuration, requests can be made like so:

client.update("I'm tweeting with @gem!")

1) Where do you put the keys config block? I've seen some guides put it in a separate file, like config/twitter_credentials.rb, omniauth.rb, or devise.rb. If I'm using devise should all 4 keys be in the devise file?

2) when the .update method is called, who tweets the tweet? The app account or the logged in user consuming my site?

3) are the access tokens what allows users to tweet from their account, and not the app's account?

Code showing how to let users tweet from their own account would be appreciated, as they seem to be missing from the readme.

Answers


  1. It doesn't really matter where you put the initializer code. But usually you put code relative to a certain gem in an initializer with the same name as the gem. So in this case, I'd put that code inside a file called twitter.rb. However, in this case I don't think you actually need an initializer.

  2. When the update method is called, the user to whom the access_token and access_token_secret belong is the one who tweets.

  3. Yes, the access_token is what allows users to tweet from their account. You can get that token and the corresponding secret from omniauth-twitter's response.

    omniauth = env['omniauth.auth']
    user.access_token = omniauth.credentials.token
    user.access_token_secret = omniauth.credentials.secret
    user.save
    

    Of course, to be able to save the token and the secret you need to add the appropriate fields (access_token and access_token_secret) to your user table.

You can then tweet from a controller, like so:

class SomeController < ApplicationController
  def some_action
    client = Twitter::REST::Client.new do |config|
      config.consumer_key        = ENV["TWITTER_CONSUMER_KEY"]
      config.consumer_secret     = ENV["TWITTER_CONSUMER_SECRET"]
      config.access_token        = current_user.access_token
      config.access_token_secret = current_user.access_token_secret
    end
    client.update("I'm tweeting with @gem!")
  end
end

Need Your Help

Why can't write full length string (char) in a binary file in Turbo C++

c++ binaryfiles file-handling turboc++

In an array like int a[5] we can store 5 values from a[0] to a[4]. not this..?

$.trim() jQuery not working in IE

javascript jquery internet-explorer

I am having issues with using $.trim() in IE it works just fine in firefox. Does anyone see any reason this would fail. Thank you.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.