简体   繁体   中英

Using Twitter api to render profile image on rails app

I've been trying to render a users profile picture when they login with the twitter api and am having some difficulty. No errors at this point but the image isn't rendering.

gem 'twitter' is installed.

Here is my user.rb file

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :omniauthable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

has_many :lines

def profile_image_uri(size = :mini)
parse_encoded_uri(insecure_uri(profile_image_uri_https(size))) unless @attrs[:profile_image_url_https].nil?
end

def self.find_for_twitter_oauth(auth, signed_in_resource=nil)


    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    if user
      return user
    else
      registered_user = User.where(:email => auth.uid + "@twitter.com").first
      if registered_user
        return registered_user
      else

        user = User.create(name:auth.extra.raw_info.name,
                            provider:auth.provider, 
                            profile_image_url:auth.uid + "https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_mini.png",
                            uid:auth.uid,
                            email:auth.uid+"@twitter.com",
                            password:Devise.friendly_token[0,20],
                          )
        end

    end
  end

Here is the relevant code from application.html.erb file

  <% if user_signed_in? %>
   <li> <a href="#">  <img src="<%= current_user.profile_image_url %>"> <%= current_user.name %>  </a></li><li> 
    <%= link_to "Sign out", destroy_user_session_path,:method => :delete %></li>
    <% else %>
    <li><%= link_to user_omniauth_authorize_path(:twitter) do %>
          <img src="https://g.twimg.com/dev/sites/default/files/images_documentation/sign-in-with-twitter-link.png">
        <% end %>

    </li>

Here is my schema.rb file from my db. I'm storing the url as a string profile_image_url.

 create_table "users", force: true do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "provider"
t.string   "uid"
t.string   "name"
t.string   "profile_image_url"

end

Any suggestions on what I'm doing wrong?

Auth Twitter Profile image url can be extracted by:

auth[:extra][:raw_info][:profile_image_url]

  user = User.create(name:auth.extra.raw_info.name,
                        provider:auth.provider, 
                        profile_image_url: auth[:extra][:raw_info][:profile_image_url],
                        uid:auth.uid,
                        email:auth.uid+"@twitter.com",
                        password:Devise.friendly_token[0,20],
                      )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM