我有这个函数来处理Fb回调。
public function handleFbCallback() {
if( !\Input::get('error', NULL) ) {
try {
$fuser = \Socialize::with('facebook')->user();
$token = $fuser->token;
if($fb = \App\UserFacebook::whereEmail($fuser->getEmail())->first()) {
$fb->fb_id = $fuser->getId();
$fb->nickname = $fuser->getNickname();
$fb->name = $fuser->getName();
$fb->avatar = $fuser->getAvatar();
$fb->token = $token;
$fb->save();
$profile = \App\Profile::whereUserId($fb->user_id)->first();
if($profile) {
$profile->name = $fuser->user['first_name'];
$profile->last_name = $fuser->user['last_name'];
$profile->save();
} else {
\App\Profile::create([
'name' => $fuser->user['first_name'],
'last_name' => $fuser->user['last_name'],
'user_id' => $fb->user_id,
]);
}
//load user and increments number login
$user = \App\User::find($fb->user_id);
if($user) {
$user->last_login = date('Y-m-d H:i:s');
$user->number_logins = $user->number_logins + 1;
$user->save();
}
}
else {
$password = str_random(8);
$nuser = \App\User::whereEmail($fuser->getEmail())->first();
if(!$nuser) {
$nuser = \App\User::create([
'email' => $fuser->getEmail(),
'password' => bcrypt($password),
'active' => 1,
]);
if(\Session::get('source', NULL)) {
$nuser->source = \Session::get('source');
\Session::forget('source');
}
if(\Session::get('campaign', NULL)) {
$nuser->source = \Session::get('campaign');
\Session::forget('campaign');
}
$nuser->save();
//profile
\App\Profile::create([
'name' => $fuser->user['first_name'],
'last_name' => $fuser->user['last_name'],
'user_id' => $nuser->id,
]);
}
$nuser->last_login = date('Y-m-d H:i:s');
$nuser->number_logins = 1;
$nuser->save();
$fb = \App\UserFacebook::create([
'fb_id' => $fuser->getId(),
'nickname' => $fuser->getNickname(),
'name' => $fuser->getName(),
'email' => $fuser->getEmail(),
'avatar' => $fuser->getAvatar(),
'token' => $token,
'user_id' => $nuser->id
]);
}
\Auth::loginUsingId($fb->user_id);
if(\Session::get('custom_url', NULL) == 'thanks') {
return redirect()->route('landing.thanks', array('social', $fb->user_id));
} elseif($url = \Session::get('custom_url', NULL)) {
\Session::forget('custom_url');
return redirect($url);
}
return redirect()->intended();
}
catch(Exception $e) {
dd($e->getMessage());
}
}
return redirect('/');
}
运行此功能会产生异常:
客户端错误:GET https://graph.facebook.com/v2.6/me?access_token=&appsecret_proof=cb32db5fac27b922d1a9c3040772a05b9a6e79f8145ee5a9fc21bbefd1f00909&fields=name,first_name,last_name,email,gender,verified
导致400 Bad Request
响应:{"错误":{"消息":"活动访问令牌必须用于查询有关当前用户的信息。","键入":" OAuthExce(截断...)
但它对令牌有例外。 有人有任何想法解决这个问题吗? 谢谢!