Facebook graph api RubyonRails6应用程序,如何从facebook获得使用相同应用程序的朋友?
为了在应用程序中进行授权,我通过gem'omniauth facebook'使用facebook登录。但目前还无法确定如何获取同时使用我的应用程序(已登录)的朋友列表,并将结果显示在单独的页面上。并在应用程序中共享他的资源(在示例书签中) 我的档案Facebook graph api RubyonRails6应用程序,如何从facebook获得使用相同应用程序的朋友?,facebook-graph-api,omniauth-facebook,ruby-on-rails-6,facebook-friends,Facebook Graph Api,Omniauth Facebook,Ruby On Rails 6,Facebook Friends,为了在应用程序中进行授权,我通过gem'omniauth facebook'使用facebook登录。但目前还无法确定如何获取同时使用我的应用程序(已登录)的朋友列表,并将结果显示在单独的页面上。并在应用程序中共享他的资源(在示例书签中) 我的档案 source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.3' gem 'rails', '~&
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.3'
gem 'rails', '~> 6.0.0'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'omniauth-facebook'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'dotenv-rails'
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
源代码'https://rubygems.org'
git|u源(:github){| repo |“https://github.com/#{repo}.git}
ruby“2.6.3”
gem'rails',“~>6.0.0”
宝石'pg','>=0.18','<2.0'
宝石“彪马”,“大于3.11”
gem'sass rails',“~>5”
gem'webpacker',“~>4.0”
gem“涡轮链接”,“大于5”
gem'jbuilder',“~>2.7”
gem'bootsnap','>=1.4.2',require:false
gem'omniauth facebook'
小组:开发,:测试
gem'byebug',平台:[:mri,:mingw,:x64_mingw]
gem'dotenv rails'
结束
小组:发展怎么办
gem“web控制台”,“>=3.3.0”
gem'listen','>=3.0.5','<3.2'
宝石“春天”
gem‘SpringWatcherListen’,“~>2.0.0”
结束
组:测试do
#增加了对Capybara系统测试和selenium驱动程序的支持
gem‘水豚’,“>=2.15”
gem“selenium webdriver”
#轻松安装和使用web驱动程序以使用浏览器运行系统测试
gem“webdrivers”
结束
#Windows不包括zoneinfo文件,因此捆绑tzinfo数据
gem'tzinfo data',平台:[:mingw,:mswin,:x64_mingw,:jruby]
用户模型
class User < ApplicationRecord
has_many :bookmarks, dependent: :destroy
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth['provider']
user.uid = auth['uid']
if auth['info']
user.name = auth['info']['name'] || ""
user.email = auth['info']['email'] || ""
end
end
end
end
class用户
会话控制器
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.where(:provider => auth['provider'],
:uid => auth['uid']).first || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end
def new
redirect_to '/auth/facebook'
end
def destroy
reset_session
redirect_to root_url, notice => 'Signed out'
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
def authenticate
redirect_to :login unless user_signed_in?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!!current_user
end
end
class sessioncontrollerauth['provider'],
:uid=>auth['uid'])。第一个| |用户。使用_omniauth(auth)创建
会话[:user\u id]=user.id
将\u重定向到root\u url,:notice=>“已登录!”
结束
def新
重定向到“/auth/facebook”
结束
def销毁
重置会话
将\重定向到根\ url,注意=>“已注销”
结束
结束
书签控制器
class BookmarksController < ApplicationController
before_action :authenticate
before_action :set_bookmark, only: [:show, :edit, :update, :destroy, :bookmark_owner]
before_action :bookmark_owner, only: [:show, :edit, :update, :destroy]
def index
@bookmarks = current_user.bookmarks
end
def show
end
def new
@bookmark = current_user.bookmarks.build
end
def edit
end
def create
@bookmark = current_user.bookmarks.build(bookmark_params)
respond_to do |format|
if @bookmark.save
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
format.json { render :show, status: :created, location: @bookmark }
else
format.html { render :new }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @bookmark.update(bookmark_params)
format.html { redirect_to @bookmark, notice: 'Bookmark was successfully updated.' }
format.json { render :show, status: :ok, location: @bookmark }
else
format.html { render :edit }
format.json { render json: @bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
@bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url, notice: 'Bookmark was successfully destroyed.' }
format.json { head :no_content }
end
end
def bookmark_owner
unless @bookmark.user_id == current_user.id
flash[:notice] = 'Access denied as you are not owner'
redirect_to bookmarks_path
end
end
private
def set_bookmark
@bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.fetch(:bookmark, {}).permit(:name, :url)
end
end
class BookmarksController
应用控制器
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.where(:provider => auth['provider'],
:uid => auth['uid']).first || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end
def new
redirect_to '/auth/facebook'
end
def destroy
reset_session
redirect_to root_url, notice => 'Signed out'
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
def authenticate
redirect_to :login unless user_signed_in?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def user_signed_in?
!!current_user
end
end
class ApplicationController
我将非常感谢您的帮助:什么可以用于这些目的。试试看——您可以在这里查阅