Pavol Havlik Pavol Havlik - 25 days ago 13
PHP Question

Oriceon OAuth Laravel 5 - Call to undefined method OAuth::consumer()

I am implementing OAuth service provider for Laravel 5 oriceon/oauth-5-laravel

I am getting this error "Call to undefined method OAuth::consumer()".
I was following the readme steps for installation:


  • I have added "oriceon/oauth-5-laravel": "dev-master" into composer.json. Then I run composer update, no errors.

  • I have added "Artdarek\OAuth\OAuthServiceProvider::class," into 'providers' array app.php

  • I have added "'OAuth' => Artdarek\OAuth\Facade\OAuth::class," into 'aliases' array in app.php



Here is my config/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'],
],

]

];


Here is my 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);
}
}
}

Answer

The problem lies in file /vendor/oriceon/oauth-5-laravel/src/Artdarek/OAuth/OAuthServiceProvider.php.

The line

$this->app['oauth'] = $this->app->share(function ($app)

has to be replaced with

$this->app['oauth'] = $this->app->instance(function ($app)

because of changing the name of the function in Laravel higher versions