我是 Rails 和 Microsoft Graph/Omniauth 等的完全新手,并且有点卡住了。
我已经按照Microsoft Graph 教程逐字逐句地遵循了https://docs.microsoft.com/en-us/graph/tutorials/ruby Build Ruby on Rails 应用程序。在添加 Azure AD 身份验证实现登录,我开始启动服务器应该重定向到https://login.microsoftonline.com我得到:
OmniAuth::AuthenticityError Forbidden.
当显示上述错误时,我没有被定向到登录我的 URL 是:http://localhost:3000/auth/microsoft_graph_auth。
我正在运行 Ruby 2.7。而不是 2.6.6(如文档中所述)。
有没有人有任何指示和/或建议?
提前致谢!
Application Trace | Framework Trace | Full Trace
omniauth (2.0.1) lib/omniauth/authenticity_token_protection.rb:27:in `deny'
rack-protection (2.1.0) lib/rack/protection/base.rb:54:in `react'
omniauth (2.0.1) lib/omniauth/authenticity_token_protection.rb:18:in `call!'
omniauth (2.0.1) lib/omniauth/authenticity_token_protection.rb:11:in `call'
omniauth (2.0.1) lib/omniauth/strategy.rb:236:in `request_call'
omniauth (2.0.1) lib/omniauth/strategy.rb:192:in `call!'
omniauth (2.0.1) lib/omniauth/strategy.rb:169:in `call'
omniauth (2.0.1) lib/omniauth/builder.rb:45:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack …Run Code Online (Sandbox Code Playgroud) 我使用omniauth使用设计并使用Facebook创建登录,但是有丢失设计帮助方法访问的问题,如current_user和user_signed_in?方法不起作用.
def create
omniauth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) || User.create_with_omniauth(omniauth)
session[:user_id] = user.id
redirect_to dashboard_path(user.id), :notice => "Signed in!"
end
Run Code Online (Sandbox Code Playgroud)
redirect_to USercontroller仪表板方法
before_filter :logged_in
def dashboard
@user = User.find(params[:id])
@comment = Comment.new
@comments = @user.comments.all.paginate(:page => params[:page], :per_page => 5)
end
Run Code Online (Sandbox Code Playgroud)
所以这里控制应该在检查ApplicationController中的logged_in方法后转到dashboard方法
ApplicationController中的logged_in方法
def logged_in
if user_signed_in?
return true
else
redirect_to root_path
flash[:message] = "please login"
end
end
Run Code Online (Sandbox Code Playgroud)
当我使用Facebook在控制台生成的代码后登录时
Started GET "/users/52/dashboard" for 127.0.0.1 at Thu Mar 29 12:51:55 +0530 2012
Processing by UsersController#dashboard …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置fb应用程序ID(由omniauth使用),以便其全局设计初始化程序和控制器.
更多信息
我需要它在设计初始化(config/initializer/devise.rb),以便我可以设置config.omniauth:facebook,fb_app_id,fb_app_secret
试图解决方案
我尝试将此信息添加到yml文件(http://railscasts.com/episodes/85-yaml-configuration-file)中,但是,看起来像是为控制器设置了yml哈希,但我无法将其用于设计初始化.
/home/ubuntu/myapp/config/initializers/devise.rb:215:in `block in <top (required)>': uninitialized constant APP_CONFIG (NameError)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我试图在rails应用程序中使用omniauth-twitter,但我一开始就陷入困境,无法弄清楚原因是什么.我确实在dev.twitter.com注册了我的应用程序,我得到了CONSUMER_KEY和CONSUMER_SECRET.为了缩短测试时间,我将这两个变量直接放在initializers/omniauth.rb中
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "an_alphanumeric_code", "some_other_alphanumeric_code"
end
Run Code Online (Sandbox Code Playgroud)
在application.html.erb布局中,我有Twitter登录的链接
<%= link_to "Sign in with Twitter", "auth/twitter" %>
Run Code Online (Sandbox Code Playgroud)
但是当我点击登录时,我没有被重定向到Twitter登录
401 Unauthorized
Run Code Online (Sandbox Code Playgroud)
我已经在SO上检查了其他问题,这不应该是计算机时间问题(我的rails服务器时间设置正确).我怀疑问题在于没有可用于测试的公共IP和服务器名称.
是否有人能够从本地127.0.0.1:3000 rails服务器上运行omniauth-twitter?
我正在使用OmniAuth-Facebook并将其初始化为mysubapp/app.rb:
require 'omniauth-facebook'
class MySubApp < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
enable :sessions
SCOPE = 'email,read_stream'
ENV['APP_ID'] = '111111111111111'
ENV['APP_SECRET'] = '11111111111111111111111111111111'
use OmniAuth::Builder do
provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => SCOPE
end
end
Run Code Online (Sandbox Code Playgroud)
我只是想知道这是否应该与Padrino合作.这是放置我的初始化器的正确位置,或者,您可以创建一个特定的子应用程序config.ru?
我有一个使用Omni-Auth GitHub宝石的Rails应用程序.用户帐户创建和通过GitHub登录工作完美无瑕!
这是我的用户模型:
def self.from_omniauth(auth)
find_by_github_uid(auth["uid"]) || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.github_uid = auth["uid"]
user.github_token = auth["credentials"]["token"]
user.username = auth["info"]["nickname"]
user.email = auth["info"]["email"]
user.full_name = auth["info"]["name"]
user.gravatar_id = auth["extra"]["raw_info"]["gravatar_id"]
user.blog_url = auth["extra"]["raw_info"]["blog"]
user.company = auth["extra"]["raw_info"]["company"]
user.location = auth["extra"]["raw_info"]["location"]
user.hireable = auth["extra"]["raw_info"]["hireable"]
user.bio = auth["extra"]["raw_info"]["bio"]
end
end
Run Code Online (Sandbox Code Playgroud)
但有时用户改变他们的生物或公司,或者他们想要被雇用,所以我不想删除旧帐户,我认为如果在更新帐户后及时更新它们会很好.
这样做的最佳做法是什么?如何使用现有的OmniAuth代码更新用户信息?
我正在登录页面上为 LinkedIn 实施 OmniAuth,但出现此错误:
SyntaxError
/app/models/user.rb:9: syntax error, unexpected '\n', expecting =>
Run Code Online (Sandbox Code Playgroud)
它正在查看的代码是这样的:
class User < ActiveRecord::Base
attr_accessor :email, :password, :password_confirmation
VALID_EMAIL_REGEX = /([\w+.]+)@[a-z0-9\-.]+\.[a-z]+/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
devise :rememberable, :omniauthable, :omniauth_providers => [:linkedin], :trackable
end
Run Code Online (Sandbox Code Playgroud)
请问有什么建议吗?
我想在 oauth 登录后重定向回当前页面,我遵循了这个 Devise wiki并执行了以下操作:
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
request.env['omniauth.origin'] || stored_location_for(resource) || request.referer || root_path
end
end
Run Code Online (Sandbox Code Playgroud)
request.env['omniauth.origin']是http://www.bubutravel.com/users/sign_in
stored_location_for(resource) 是零
request.referer是http://openapi.qzone.qq.com/oauth/[omitted](我的提供者)
因此,在我的 omniauth 登录后,我再次被重定向到提供程序的 URL。
维基过时了吗?将 omniauth 登录重定向到当前页面的推荐方法是什么?
我正在编写自定义 OAuth2 策略。我需要向expired?哈希响应添加一个方法,但我不知道该怎么做。我得到的响应给了我一个时间直到到期,但不是过期的方法本身。
我需要添加一个过期的?方法,不知道如何。有什么帮助吗?