1

I have successfully integrated my application with omniauth and my twitter account. I was able to grab the name, provider, uID, location, avatar and URL for each user that logs in.

But when I try to grab a list of the users followers I am not able to. I have tried using this documentation https://github.com/arunagw/omniauth-twitter#authentication-hash

my User.rb

class User < ApplicationRecord
  class << self
  def from_omniauth(auth_hash)
    user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
    user.name = auth_hash['info']['name']
    user.location = auth_hash['info']['location']
    user.image_url = auth_hash['info']['image']
    user.url = auth_hash['info']['urls']['Twitter']
    user.save!
    user
  end
end
end

I tried adding this to my user.rb unsuccessfully

 # user.followers = auth_hash['extra']['raw_info']['followers_count']

in my index.html.erb

<% if current_user %>
  <div class="page-header">
    <h1 class="">Here is some info about you...</h1>
  </div>
  <div class="panel panel-default">
    <div class="panel-body">
      <ul>
        <span><strong>Name:</strong> <%= current_user.name %></span><br>
        <span><strong>Provider:</strong> <%= current_user.provider %></span>
        <li><strong>uID:</strong> <%= current_user.uid %></li>
        <li><strong>Location:</strong> <%= current_user.location %></li>
        <li><strong>Avatar URL:</strong> <%= image_tag current_user.image_url, alt: current_user.name %></li>
        <li><strong>URL:</strong> <%= current_user.url %></li>
        **<li><strong>Followers:</strong> <%= current_user.followers_count %></li>**
      </ul>
    </div>
  </div>
<% else %>
  <div class="jumbotron">
  <h1 class="text-center">Welcome!</h1>
  <p>Authenticate via twitter to get started.</p>
</div>
<% end %>

Ive tried first taking the Followers_count as-well just for testing but was unable to.

Any insights on how I should proceed? or what I should add to my users model?

schema.rb

  create_table "users", force: :cascade do |t|
    t.string "provider", null: false
    t.string "uid", null: false
    t.string "name"
    t.string "location"
    t.string "image_url"
    t.string "url"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "followers_count"
  end

end
Mark
  • 11
  • 1
  • 1
    Have you tried to use the twitter API - `https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list` or you could integrate your existing app with this? `https://github.com/sferik/twitter` – Kedarnag Mukanahallipatna Nov 28 '19 at 04:23
  • @KedarnagMukanahallipatna is right and should submit the comment above as the answer. The auth process from the omniauth gem only gives you the users information and tokens to interact with twitter's api. You need to do what he suggests in orders to get the followers list. – Int'l Man Of Coding Mystery Nov 28 '19 at 08:40

0 Answers0