facebook-graph-api - Ruby on rails 6 应用程序,如何从 facebook 获取使用相同应用程序的朋友?
问题描述
对于应用程序中的授权,我使用通过 facebook 登录,通过 gem 'omniauth-facebook'。但是目前卡住了如何获取也使用我的应用程序(登录)的朋友列表,并将结果显示在单独的页面上。并在应用程序中分享他的资源(在示例书签中)
我的宝石文件
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]
用户模型
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 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 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 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
我将不胜感激:什么可以用于这些目的。
解决方案
推荐阅读
- python - 需要登录的 Python 网页抓取页面
- php - Laravel EventListener 不会触发
- c# - 调用基本构造函数时如何使用“this”关键字
- javascript - 在nodejs api rest中按字符串查询时出错
- uml - 如何为我没有编码的大型项目创建 UML 图?
- python - 如何使用 pyside2 在 Qtableview 中显示(和格式化)日期时间数据
- javascript - 如何从请求中返回此函数中的 var?
- azure-devops - 如何从 Power bi 中的 Azure DevOps 查询工作项的状态更改
- angular - CORS 策略已阻止对 XMLHttpRequest 的访问。请求的资源上不存在“Access-Control-Allow-Origin”标头
- apache-nifi - 如何配置 PutFile 使其按 24 小时计划运行,但运行时间与传入流文件的数量一样多