ruby-on-rails – 用于Rails API中的提供程序身份验证的Omniauth

ruby-on-rails – 用于Rails API中的提供程序身份验证的Omniauth,第1张

概述我已经让omniauth在我的网络应用程序上完美运行.我还为我们的iPhone应用程序创建了一个API进行交互,我试图让omniauth工作. 有没有办法将访问令牌(从与Facebook.app的集成iOS集成接收)传递给omniauth以在数据库中创建提供者条目? 现在在我的网络应用程序中,我有一个带有以下代码的身份验证控制器 def create omniauth = request. 我已经让omniauth在我的网络应用程序上完美运行.我还为我们的iPhone应用程序创建了一个API进行交互,我试图让omniauth工作.

有没有办法将访问令牌(从与Facebook.app的集成iOS集成接收)传递给omniauth以在数据库中创建提供者条目?

现在在我的网络应用程序中,我有一个带有以下代码的身份验证控制器

def create    omniauth = request.env["omniauth.auth"]    user = User.where("authentications.provIDer" => omniauth['provIDer'],"authentications.uID" => omniauth['uID']).first    if user      session[:user_ID] = user.ID      flash[:notice] = t(:signed_in)      redirect_to root_path    elsif current_user      user = User.find(current_user.ID)      user.apply_omniauth(omniauth)      user.save      flash[:notice] = t(:success)      redirect_to root_path    else      session[:omniauth] = omniauth.except('extra')      flash[:notice] = "user not found,please signup,or login. Authorization will be applIEd to new account"      redirect_to register_path    end  end
解决方法 在我的API用户控制器中,我创建了以下内容:

def create    @user = User.new(params[:user])    @user.save    # Generate data for omni auth if they're a facebook user    if params[:fb_access_token]      graph = Koala::Facebook::API.new(params[:fb_access_token])      profile = graph.get_object('me')      @user['fb_ID'] = profile['ID']      @user['fb_token'] = params[:fb_access_token]      @user['gender'] = profile['gender']      # Generate omnihash      omnihash = Hash.new      omnihash['provIDer'] = 'facebook'      omnihash['uID'] = profile['ID']      omnihash['info'] = Hash.new      omnihash['info']['nickname'] = profile['username']      omnihash['info']['name'] = profile['name']      omnihash['info']['email'] = profile['email']      omnihash['info']['first_name'] = profile['first_name']      omnihash['info']['last_name'] = profile['last_name']      omnihash['info']['verifIEd'] = profile['verifIEd']      omnihash['info']['urls'] = Hash.new      omnihash['info']['urls']['Facebook'] = profile['link']      omnihash['credentials'] = Hash.new      omnihash['credentials']['token'] = params[:fb_access_token]      omnihash['extra'] = Hash.new      omnihash['extra']['raw_info'] = Hash.new      puts omnihash      # Save the new data      @user.apply_omniauth(omnihash)      @user.save    end
总结

以上是内存溢出为你收集整理的ruby-on-rails – 用于Rails API中的提供程序身份验证的Omniauth全部内容,希望文章能够帮你解决ruby-on-rails – 用于Rails API中的提供程序身份验证的Omniauth所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1274689.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存