【发布时间】:2017-03-20 14:31:01
【问题描述】:
我正在为 Laravel 5 oriceon/oauth-5-laravel 实现 OAuth 服务提供者
我收到此错误“调用未定义的方法 OAuth::consumer()”。 我按照自述步骤进行安装:
我在 composer.json 中添加了 "oriceon/oauth-5-laravel": "dev-master"。然后我运行 composer update,没有错误。
我已将“Artdarek\OAuth\OAuthServiceProvider::class”添加到 'providers' 数组 app.php 中
我已将“'OAuth' => Artdarek\OAuth\Facade\OAuth::class”添加到 app.php 中的“别名”数组中
这是我的配置/oauth-5-laravel.php:
return [
/*
|--------------------------------------------------------------------------
| oAuth Config
|--------------------------------------------------------------------------
*/
/**
* Storage
*/
'storage' => '\\OAuth\\Common\\Storage\\Session',
/**
* Consumers
*/
'consumers' => [
'Facebook' => [
'client_id' => '',
'client_secret' => '',
'scope' => [],
],
'Google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_SECRET_ID'),
'scope' => ['userinfo_email', 'userinfo_profile'],
],
]
];
这是我的 GoogleController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use OAuth;
class GoogleController extends Controller
{
public function loginWithGoogle(Request $request)
{
// get data from request
$code = $request->get('code');
// get google service
$googleService = \OAuth::consumer('Google');
// check if code is valid
// if code is provided get user data and sign in
if ( ! is_null($code))
{
// This was a callback request from google, get the token
$token = $googleService->requestAccessToken($code);
// Send a request with it
$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
$message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
echo $message. "<br/>";
//Var_dump
//display whole array.
dd($result);
}
// if not ask for permission first
else
{
// get googleService authorization
$url = $googleService->getAuthorizationUri();
// return to google login url
return redirect((string)$url);
}
}
}
【问题讨论】: