我正在使用Laravel 4的laravel-oauth2軟件包來設置Facebook登錄。我正在使用這裏看到的包:https://github.com/madewithlove/laravel-oauth2Laravel-Oauth2問題Laravel 4
當運行控制登錄頁面,公共/ oauth/Facebook,Facebook提示符顯示正常,但是當我點擊「接受」,頁面吐出以下錯誤:
ErrorException
Argument 1 passed to OAuth2\Provider\Facebook::get_user_info() must be an instance of OAuth2\Token\Token_Access, instance of OAuth2\Token_Access given, called in /Applications/MAMP/htdocs/crowdsets/laravel-master/app/controllers/Oauth2Controller.php on line 36 and defined
它指向/vendor/taylorotwell/laravel-oauth2/src/OAuth2/Provider/Facebook.php: 26
在Facebook.php的get_user_info()
功能如下:
public function get_user_info(Token_Access $token)
{
$url = 'https://graph.facebook.com/me?'.http_build_query(array(
'access_token' => $token->access_token,
));
$user = json_decode(file_get_contents($url));
// Create a response from the request
return array(
'uid' => $user->id,
'nickname' => $user->username,
'name' => $user->name,
'email' => $user->email,
'location' => $user->hometown->name,
'description' => $user->bio,
'image' => 'https://graph.facebook.com/me/picture?type=normal&access_token='.$token->access_token,
'urls' => array(
'Facebook' => $user->link,
),
);
}
我Oauth2Controller.php文件看起來像這樣:
<?php
use OAuth2\OAuth2;
use OAuth2\Token_Access;
use OAuth2\Exception as OAuth2_Exception;
class Oauth2Controller extends BaseController
{
public function getIndex($provider) {
$provider = OAuth2::provider($provider, array(
'id' => '****************',
'secret' => '********************',
));
if(! isset($_GET['code'])) {
return $provider->authorize();
}
else
{
// Howzit?
try
{
$params = $provider->access($_GET['code']);
$token = new Token_Access(array(
'access_token' => $params->access_token
));
$user = $provider->get_user_info($token);
// Here you should use this information to A) look for a user B) help a new user sign up with existing data.
// If you store it all in a cookie and redirect to a registration page this is crazy-simple.
echo "<pre>";
var_dump($user);
}
catch (OAuth2_Exception $e)
{
show_error('That didnt work: '.$e);
}
}
}
}
我不知道爲什麼它是給了我這個錯誤,因爲我跟着文檔和教程。感謝您爲解決此問題提供的幫助。
錯誤明確指出'必須是OAuth2 \ Token \ Token_Access的實例,OAuth2 \ Token_Access的實例'在控制器中嘗試使用OAuth2 \ Token \ Token_Access; '代替'使用OAuth2 \ Token_Access;' –
當我這樣做時,它說:Class'OAuth2 \ Token \ Token_Access'找不到 – user1072337
然後嘗試'use \ Token_Access;'它會使用全局可用的Token_Access類 –