Laravel Socialite: Missing required parameter: redirect_uri - php

I am using Laravel 5.3 with Socialite.
I am getting 400 Bad Request error with message:
Missing required parameter: redirect_uri
The URL it generates is:
https://accounts.google.com/o/oauth2/auth?scope=openid+profile+email&response_type=code&state=RYPmT1B93CaUdi44Z7iwfmRPx3hIy7an7yxAVY9l
Details below:
service.php
'google' => [
'client_id' => '1234455-u3ifk8tr1qs41487fmevg2h2s1v6ubue.apps.googleusercontent.com',
'client_secret' => 'DxSOS0p1xKNuPger3IS_E4-i',
'redirect' => 'http://localhost:8000/google/callback',
],
Routes
Route::get('/{provider}/redirect', 'Auth\RegisterController#redirectToProvider');
Route::get('/{provider}/callback', 'Auth\RegisterController#handleProviderCallback');
Authorized URI settings in Google Console
http://localhost:8000/google/callback
Controller
public function handleProviderCallback($provider)
{
try {
$social_user = Socialite::driver($provider)->user();
} catch (Exception $e) {
return redirect('/');
}
}

Just double check your service.php file, env file and api setting in google console
Example (no env in redirect):
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => 'http://localhost:8000/api/login/google/callback'
],

Related

Laravel Socialite Facebook Login Error: The parameter app_id is required

Trying to get socialite to work on my app. Facebook returns the The parameter app_id is required error.
Routes:
Route::get('/login/facebook', '\CommendMe\Http\Controllers\AuthController#redirectToProvider');
Route::get('/login/facebook/callback', '\CommendMe\Http\Controllers\AuthController#handleProviderCallback');
services.php:
'facebook' => [
'client_id' => env('426129694395672'),
'client_secret' => env('840fca14fc9fac4b592cd49f285c2ee9'),
'redirect' => 'http://localhost/login/facebook/callback',
],
AuthController.php
public function redirectToProvider() {
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback() {
$user = Socialite::driver('facebook')->user();
$user->name;
}
When trying the /login/facebook route, facebook returns this error.
Why is this happening?
Either use as
'client_id' => '426129694395672',
Or
'client_id' => env("FB_APP",'426129694395672'),
and use FB_APP = '426129694395672' in .env file
Instead
'client_id' => env('426129694395672'),
Using env('VarName') is to get value of environment variable named as VarName in .env file
Assuming that you have the following in your .env file:
CLIENT_ID=426129694395672
CLIENT_SECRET=840fca14fc9fac4b592cd49f285c2ee9
The facebook[] in your services.php should be like this:
'facebook' => [
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'redirect' => 'http://localhost/login/facebook/callback',
],

Laravel | LinkedIn and Socialite - Not passing parameters

I'm having an issue with socialite on Laravel, I want to add a LinkedIn login on my website, I already added a Twitter login which is 100% working. My issue is that when I do return Socialite::driver('linkedin')->redirect(); , it redirect me to a link like this:
https://www.linkedin.com/oauth/v2/authorization?client_id=&redirect_uri=&scope=r_basicprofile+r_emailaddress&response_type=code&state=XXX
And I get an error because as you can see, the URL doesn't have the client_id and redirect_uri parameters..
If I had them manually, everything work. I checked my .env file, my services.php and my SocialAuthController too.. I can't figure out.. Here is my .env file:
LINKEDIN_ID=XXX123XXX123XXX
LINKEDIN_SECRET=XXX456XXX456XXX
My services file:
'linkedin' => [
'client_id' => env('LINKEDIN_ID'),
'client_secret' => env('LINKEDIN_SECRET'),
'redirect' => env('APP_URL') . '/oauth/linkedin/callback',
],
My SocialAuthController:
public function redirectToProvider($driver)
{
if ($driver === 'linkedin')
{
return Socialite::driver('linkedin')->redirect();
}
}
And finally my routes:
Route::get('auth/social', 'Auth\SocialAuthController#show')->name('social.login');
Route::get('login/{driver}', 'Auth\SocialAuthController#redirectToProvider')->name('social.oauth');
Route::get('login/{driver}/callback', 'Auth\SocialAuthController#handleProviderCallback')->name('social.callback');
I did everything like this for the Twitter login and it's working perfectly..
Here is my solution:
web.php
Route::get('/redirect', 'Auth\LoginController#redirectToProvider');
Route::get('/callback', 'Auth\LoginController#handleProviderCallback');
Route::get('/shopping', 'GoogleShopping\GShoppingController#index');//redirect to home
config/services.php
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_APP_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URL'),//it must be same the home url
]
Controllers/Auth/LoginController.php
try {
$user = Socialite::driver('google')->user();
} catch (\Exception $e) {
return view('/');
}
return redirect()->to('/shopping');
Is very important clean the cache php artisan config:cache and php artisan cache:clear

Instagram login issue in Laravel

I try to login by Instagram , and I refer mbarwick83/instagram.
When I try to login,it have some problem,only show some url:
https://api.instagram.com/oauth/authorize?response_type=code
And go to url,have some message:
// 20171025142046
// https://www.instagram.com/oauth/authorize?response_type=code
{
"error_type": "OAuthException",
"code": 400,
"error_message": "You must include a valid client_id, response_type, and redirect_uri parameters"
}
But I already setting "client_id" and "redirect_uri".
How can I fix this problem?Thanks.
Step by Step:
1.Install mbarwick83/instagram
composer require mbarwick83/instagram
Add something in config/app.php
providers:Mbarwick83\Instagram\InstagramServiceProvider::class
aliases:'Instagram'=> Mbarwick83\Instagram\Facades\Instagram::class
Include 'Mbarwick83' in this project:
use Mbarwick83\Instagram\Instagram;
4.To publish the packages configuration file
php artisan vendor:publish
5.Add Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mbarwick83\Instagram\Instagram;
use App\Http\Requests;
class Mbarwick83Controller extends Controller
{
public function index(Instagram $instagram)
{
return $instagram->getLoginUrl();
}
public function callback(Request $request, Instagram $instagram)
{
$response = $instagram->getAccessToken($request->code);
// or $response = Instagram::getAccessToken($request->code);
if (isset($response['code']) == 400)
{
throw new \Exception($response['error_message'], 400);
}
return $response['access_token'];
}
}
6.Setting "client_id" and "redirect_uri":
Path: ../vendor/mbarwick83/instagram/src/config
<?php
return [
'client_id' => env('0264df467XXXXXX'),
'client_secret' => env('6e5d9XXXXX4eeXX1'),
'redirect_uri' => env('http://localhost/login/Instagram/callback'),
'scopes' => 'basic public_content'
];
7.Setting ../routes/web.php
Route::get('login/Instagram', 'Mbarwick83Controller#index');
Route::get('login/Instagram/callback', 'Mbarwick83Controller#callback');
8.Setting ../.env
INSTAGRAM_CLIENT_ID=0264df467XXXXXX
INSTAGRAM_CLIENT_SECRET=6e5d9XXXXX4eeXX1
INSTAGRAM_CALLBACK_URL=http://localhost/login/Instagram/callback
Change your config file, remove the env function
return [
'client_id' => '0264df467XXXXXX',
'client_secret' => '6e5d9XXXXX4eeXX1',
'redirect_uri' => 'http://localhost/login/Instagram/callback',
'scopes' => 'basic public_content'
];
You're using env() wrong, it requires a key name, not the value. (Though you could set a "default value" as second parameter)
<?php
return [
'client_id' => env('INSTAGRAM_CLIENT_ID'),
'client_secret' => env('INSTAGRAM_CLIENT_SECRET'),
'redirect_uri' => env('INSTAGRAM_CALLBACK_URL'),
'scopes' => 'basic public_content'
];
env('INSTAGRAM_CLIENT_ID') will get the value of INSTAGRAM_CLIENT_ID from your .env file.

Laravel server hangs whenever I try to request localhost:8000/any using guzzle

If I make any request to http://localhost:8000 or http://127.0.0.1:8000 it hangs on status pending. (Exactly as here https://github.com/guzzle/guzzle/issues/1857)
I was told that it isn't related to guzzle and that I should better ask about it here.
I stumbled upon this problem while following laravel.com/docs/5.4/passport
This is the code that hangs:
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'redirect_uri' => 'http://example.com/callback',
'code' => $request->code,
],
]);
I tried making GET and POST request to working API routes (tested with postman) and it still hangs when calling the same routes using guzzle.
So is there a way to make requests to my own API while using php artisan serve?
Carl has a great solution to this. If you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.
Then you would update your post request to:
$response = $http->post('http://localhost:8001/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'redirect_uri' => 'http://example.com/callback',
'code' => $request->code,
],
]);
This should help during your testing until you are able to everything on server or a local virtual host.
try this.
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\User;
class UserController extends Controller
{
//use AuthenticatesUsers;
protected function login(Request $request)
{
$request->request->add([
'grant_type' => 'password',
'client_id' => '3',
'client_secret' => '6BHCRpB4tpXnQvC1DmpT7CXCSz7ukdw7IeZofiKn',
'scope' => '*'
]);
// forward the request to the oauth token request endpoint
$tokenRequest = Request::create('/oauth/token','post');
return Route::dispatch($tokenRequest);
}
}
I ended up solving it by using wamp virtualhost instead of php artisan serve. No idea why it doesn't work with localhost though.
UPDATE: Someone was kind enough to explain why it wouldn't work.
In https://github.com/guzzle/guzzle/issues/1857#issuecomment-506962175
The reason for this is php artisan serve is a single thread application. So when we use guzzle to request from it to itself, it basically just tries to finish guzzle request (as a client) first then come to finish that request (as a server), which is impossible.
More info about this: https://php.net/manual/en/features.commandline.webserver.php
Also this answer:
When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.
/**
* Login function
*/
public function login(Request $request) {
/*
Sample post object
{
"username": "test#gmail.com",
"password": "test123"
}
*/
if (Auth::attempt(['email' => request('username'), 'password' => request('password')])) {
return $this->getToken($request);
}
else {
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function getToken(Request $request) {
//Get client ID and client Secret
$client = DB::table('oauth_clients')->where('password_client',1)->first();
$request->request->add([
"grant_type" => "password",
"username" => $request->username,
"password" => $request->password,
"client_id" => $client->id,
"client_secret" => $client->secret,
]);
// Post to "/oauth/token
$tokenRequest = $request->create('/oauth/token','post');
$instance = Route::dispatch($tokenRequest);
//return token_type, expires_in, access_token, refresh_token
return response()->json(json_decode($instance->getContent()));
}

Laravel Passport API call code parameter null

I'm trying to call a Laravel API using a Laravel Client.
Here is the code:
Route::get('/callback', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://galaxy.dev/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => '3',
'client_secret' => 'rrhaEv0B6NAFLyMFqvZrY87gkquFF2UwhAHPtd8L',
'redirect_uri' => 'http://galaxy-game.dev/callback',
'code' => $request->code,
],
]);
return json_decode((string) $response->getBody(), true);
});
I get this response from the API:
Client error: POST http://galaxy.dev/oauth/token resulted in a 400
Bad Request response: {"error":"invalid_request","message":"The
request is missing a required parameter, includes an invalid parameter
value, (truncated...)
I have noticed in the API error log this:
Client->request('post', 'http://galaxy.dev/oauth/token',
array('form_params' => array('grant_type' => 'authorization_code',
'client_id' => '3', 'client_secret' =>
'rrhaEv0B6NAFLyMFqvZrY87gkquFF2UwhAHPtd8L', 'redirect_uri' =>
'http://galaxy-game.dev/callback', 'code' => null), 'synchronous' =>
true)) in Client.php line 87
It says that
$request->code = null
Does anyone have any ideas why this might be? This seems to be the reason why it is failing. I have followed the Laravel docs exactly. Any help would be greatly appreciated!
Hi I fixed this by leaving the code value empty like this:
'code' => '',
Since $request->code is null, that probably causes that problem.

Categories