0

How can i show r_basicprofile data from omniauth-linkedin , in my layout view? Im new at rails and im trying to show this data on my application,html.erb view.

i have the next codes

app/controllers/omniauth_callbacks_controller.rb

here i tried to make my token data available for further use with

access_token = auth["credentials"]["token"]
access_secret = auth["credentials"]["secret"]

class OmniauthCallbacksController < Devise::OmniauthCallbacksController   

def linkedin

  auth = env["omniauth.auth"]
  @user = User.from_omniauth(request.env["omniauth.auth"])
  if @user.persisted?
    access_token = auth["credentials"]["token"]
    access_secret = auth["credentials"]["secret"]
    flash[:notice] = I18n.t "devise.omniauth_callbacks.success",kind:   @user['provider'].titleize
    sign_in_and_redirect @user, :event => :authentication
  else
    session["devise.linkedin_uid"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
  end
 end

end

app/models/user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
 :recoverable, :rememberable, :trackable, :validatable,:omniauthable,:omniauth_providers => [:linkedin]

 def self.from_omniauth(auth)
   user = where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.email = auth.info.email
    user.password = Devise.friendly_token[0,20]
    user.name = auth.info.name 
    user.image = auth.info.image  # assuming the user model has a name
    # If you are using confirmable and the provider(s) you use validate emails, 
    # uncomment the line below to skip the confirmation emails.
    # user.skip_confirmation!
    end
  end

end

on config/initializers/devise.rb

require 'omniauth-linkedin'


  Rails.application.config.middleware.use OmniAuth::Builder do
     provider :linkedin,
      "client_id","client_secret",
    :scope => "r_basicprofile r_emailaddress", 
    :fields => ['id', 'email-address', 'first-name', 'last-name','headline', 'location', 'industry', 'picture-urls::(original)', 'public-profile-url','positions']


  end

finally i have just my layout view rendering some of the data, but i still can't make image visible or as i do here, get just the url that should come in the auth.env request, i tried to store in user model with user.image = auth.info.image but im not able to show in the html , when i got to console user.image is nil.

I've read that after i can access to the tokens from the API, i understand that when the def self.from_omniauth(auth) function in User.rb is called from the OmniauthCallbacksController it succeds on getting a token, so how i'm not able to display at least the user.image = auth.info.image? what is lacking on my code?

by the way, im opened to redirect to another view if necessary, it's just that i do not know how to store this data , linkedin says this about additional fields:

retrieve basic profile data

if i try to hardcode the link in application.html.erb

  • <%= link_to "basic_profile", "https://api.linkedin.com/v1/people/~?format=json"%>
  • it respond whit
    {
    
        "errorCode": 0,
        "message": "Unknown authentication scheme",
        "requestId": "CA0TD716FJ",
        "status": 401,
        "timestamp": 1490505226812
    
    }
    

    here is my

    application.html.erb

    <!DOCTYPE html>
    <html>
    <head>
        <title>Oklock</title>
        <%= csrf_meta_tags %>
    
        <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
        <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    </head>
    
    <body>
        <p class="notice"><%= notice %></p>
        <p class="alert"><%= alert %></p>
        <%= yield %>
    
    
    
        <% if user_signed_in? %>
    
        <ul> 
            <li>Signed in as <%= current_user.name %>. Not you?</li>
            <li> <%= current_user.provider %></li>
            <li> <%= current_user.uid %></li>
            <li> <%= current_user.email %></li>
            <li><%= current_user.image%></li>
            <li><%= link_to "basic_profile", "https://api.linkedin.com/v1/people/~?format=json"%></li>
    
    
        </ul>
        <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
        <% else %>
        <%= link_to "Sign up", new_user_registration_path %> or
        <%= link_to "Sign in", new_user_session_path %>
        <%= link_to "Sign in with Linkedin",user_linkedin_omniauth_authorize_path %>
            <% end %>
    
    </body>
    </html>
    

    help me people! if i should change or make a different api call on link_to , how? im searching like from all day since i've started the morning and now are 02:19 am and i can't get it done.

    please help!

    0 Answers0