gpt4 book ai didi

ruby - 用于在 Sinatra 中使用 Ruby Koala gem 的网站的 Facebook Graph API

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:56 26 4
gpt4 key购买 nike

我想为网络应用程序实现 Facebook 登录。在帐户创建步骤中,我只需要用户的基本公共(public)信息。

这是我所做的:

  1. 创建了一个几乎没有自定义权限的基本 Facebook 应用。
  2. 使用Koala中的APP_IDAPP_SECRET获取access_token
  3. 一切正常,我可以登录/注销。
  4. 当我这样做时,我唯一能得到的信息是:graph.get_object('me') 是登录的用户名和一个 id (它看起来不像默认的 Facebook id)。
  5. 我对新 API 中是否发生了某些变化感到惊讶,因此我使用图形浏览器中的 access_token 在控制台中测试了 gem(默认情况下启用所有权限)。我使用相同的方法调用获取所有数据。
  6. 当我查看所有应用程序在注册时获得的内容时;我看到应用程序可以访问用户的基本信息、个人资料照片和其他公共(public)数据。

知道为什么会这样吗?看来我遗漏了一些明显的东西。代码在 Github 中可用。 .但这几乎就是它的全部:

require 'bundler'
Bundler.require :default
Dotenv.load '.env'
require_relative './app/constants.rb'

module Banana
class App < Sinatra::Base
use Rack::Session::Cookie, secret: COOKIE_SECRET
set :public_folder, File.dirname(__FILE__) + '/bower_components'

get '/' do
if logged_in?
haml :welcome_in, layout: :layout
else
haml :log_in, layout: :layout
end
end

get '/log_out' do
session['oauth'] = nil
session['access_token'] = nil

redirect '/'
end

get '/log_in' do
session['oauth'] = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, "#{request.base_url}/call_back")
redirect session['oauth'].url_for_oauth_code()
end

get '/call_back' do
begin
session['access_token'] = session['oauth'].get_access_token(params[:code])
rescue
redirect '/?error=user_denied'
end
redirect '/'
end

get '/test' do
if logged_in?
p graph.get_object("rakeshbs")
"e"
else
redirect '/'
end
end

def logged_in?
!session['access_token'].nil?
end

def toggle_access
logged_in? ? '/log_out' : '/log_in'
end

def graph
@graph ||= Koala::Facebook::API.new(session['access_token'])
end

def errored?
!params["error"].nil?
end

def user
p graph.get_connections(:me, :photos) # This is just nil
@user ||= OpenStruct.new(
name: graph.get_object("me")["name"], # All I get here is just a hash with the name and an id!
photo: 'http://semantic-ui.com/images/avatar/small/elliot.jpg'
)
end
end
end

最佳答案

您应该添加 fields 参数。

是这样的:
graph.get_object('me', { fields: 'id,first_name,last_name,gender,birthday,photos,email' })

关于ruby - 用于在 Sinatra 中使用 Ruby Koala gem 的网站的 Facebook Graph API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33390153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com