0

如何在我的布局视图中显示来自 omniauth-linkedin 的 r_basicprofile 数据?我是 Rails 的新手,我试图在我的应用程序 html.erb 视图上显示这些数据。

我有下一个代码

应用程序/控制器/omniauth_callbacks_controller.rb

在这里,我尝试使我的令牌数据可用于进一步使用

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

应用程序/模型/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

在 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

最后我只有我的布局视图渲染了一些数据,但我仍然无法使图像可见,或者像我在这里所做的那样,只获取应该在 auth.env 请求中出现的 url,我试图将其存储在用户模型中user.image = auth.info.image 但我无法在 html 中显示,当我到控制台时 user.image 为零。

我已经读过,在我可以从 API 访问令牌之后,我明白当从 OmniauthCallbacksController 调用 User.rb 中的 def self.from_omniauth(auth) 函数时,它会成功获取令牌,所以我是至少不能显示 user.image = auth.info.image?我的代码缺少什么?

顺便说一句,如果有必要,我打开重定向到另一个视图,只是我不知道如何存储这些数据,linkedin 说这个关于附加字段:

检索基本配置文件数据

如果我尝试对 application.html.erb 中的链接进行硬编码

  • <%= link_to "basic_profile", " https://api.linkedin.com/v1/people/~?format=json "%>
  • 它回应白衣

    {
    
        "errorCode": 0,
        "message": "Unknown authentication scheme",
        "requestId": "CA0TD716FJ",
        "status": 401,
        "timestamp": 1490505226812
    
    }
    

    这是我的

    应用程序.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>
    

    帮帮我的人!如果我应该更改或在 link_to 上进行不同的 api 调用,如何?自从我从早上开始到现在是凌晨 02:19,我整天都在搜索,但我无法完成。

    请帮忙!

    4

    0 回答 0