我正在尝试从 Facebook 获取朋友列表。通过 Facebook 登录不是问题,但问题是获取人的朋友 - 因为access token
.
puts request.env["omniauth.auth"].inspect
puts '==='
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
@fb_user = FbGraph::User.fetch(@user.uid).friends
puts @fb_user.inspect
问题出在第 4 行 - 在这种情况下,我遇到了错误
OAuthException :: An access token is required to request this resource.
当我把这样的东西放在那里时:
@fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends
我去拿
OAuthException :: (#803) Some of the aliases you requested do not exist: PRINTED OUT TOKEN
获得的正确方法是access token
什么?
编辑:当前流量
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
@fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends
if !@user
flash[:error] = 'This email address is already used in the system.'
redirect_to :back
elsif @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
在用户模型中:
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:provider => 'facebook', :uid => data.id).first
user
elsif user = User.where('email = ? AND provider IS NULL', data.email).first
return false
else
...saving data...
end
return user if user
end