如何在布局视图中显示omniauth-linkedin的r_basicprofile数据?我是rails的新手,我试图在我的应用程序html.erb视图中显示这些数据。
我有下一个代码
这里我尝试使用
进一步使用我的令牌数据 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
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
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请求中的网址,我试图存储用户模型 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中的链接进行硬编码
{
"errorCode": 0,
"message": "Unknown authentication scheme",
"requestId": "CA0TD716FJ",
"status": 401,
"timestamp": 1490505226812
}
这是我的
<!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,我无法完成。
请帮忙!