Laravel 8 - unsupported_grant_type - php

I have a problem with the following:
I am creating an API (I'm new with this)
With this API I want to consume another API or WebService
In Postman I make a get request to the URL and it returns OK the access_token, but in the Laravel project it returns: "error" => "unsupported_grant_type".
This is my code:
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
'content-type' => 'application/x-www-form-urlencoded'
])->post('http://url:52904/Token', [
'userName' => 'menganito',
'password' => 'pepito',
'grant_type' => 'password'
]);
dd($response->json());
I tried installing ** Laravel Passport **, but I'm not using a DB for now.

The problem was x-www-form-urlencoded.
This solved my problem (asForm)
$response = Http::asForm()->post('http://url.com', [
'userName' => 'user',
'password' => 'pass',
'grant_type' => 'password'
]);
dd($response->json());

Related

Can't make http POST request on laravel application

When i try to send http POST request to get passport token ,that request cannot processed.
$response = Http::asForm()->post('http://localhost:8000/oauth/token', [
'grant_type' => 'password',
'client_id' => env('PASSPORT_CLIENT_ID', null),
'client_secret' => env('PASSPORT_CLIENT_SECRET', null),
'username' => $request->email,
'password' => $request->password,
'scope' => '',
]);
return $response->json();
My project run on port 8000. But I am not able to make a http post request using port 8000 .
Instead of that I need to run same project on 8000 and 8001 to get result.
$response = Http::asForm()->post('http://localhost:8001/oauth/token', [
'grant_type' => 'password',
'client_id' => env('PASSPORT_CLIENT_ID', null),
'client_secret' => env('PASSPORT_CLIENT_SECRET', null),
'username' => $request->email,
'password' => $request->password,
'scope' => '',
]);
return $response->json();
This will return expected output. But I need expected output by running my project only on single port. Someone please help me.

Laravel Pusher broadcast data as array

When trying to send a request to Pusher from my controller, I would like to see a array in the channel as follows:
[3,"e7563ce0-c1ef-499d-9919-9a3d2a3ff523",{"status":"Accepted","currentTime":"2022-06-22T13:16:05+0000","interval":30}]
The way I am sending an request looks like this (ChargerController.php)
$pusher = new Pusher(
$charger->identifier,
$charger->identifier,
$charger->identifier,
[
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => false,
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
]
);
$pusher->trigger($charger->getOcppChannelName(), 'Reset', json_encode([
OCPPMessageService::OCPP_MESSAGE_TYPE_CALL,
Uuid::uuid4(),
'Reset',
['type' => ChargerResetTypeEnum::HARD],
]), [], true);
For some reason this gives the following result:
{"0":2,"1":"1a00b2f6-5af9-4a56-bd1c-723b6669380d","2":"Reset","3":{"type":"Hard"}}
I would like to have the data be an array, without any keys, as the system we are interacting with doesn't understand an object with keys. Even in the vendors we weren't able to find the reason it does this.

Fusio api cache configuration

Googled and tried,but failed.
How to configure Fusio to use redis.?
//'psx_cache_factory' => null,
This doesnt work:
'psx_cache_factory' => 'redis'
This also doesnt work:
'psx_cache_factory' => 'redis://127.0.0.1:6379',
Answered by developer.
Github
'psx_cache_factory' => function($config, $namespace){
$client = new \Predis\Client([
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
]);
return new \Doctrine\Common\Cache\PredisCache($client);
},

Multi-tenant SaaS built in Yii2

I am working in a multi-tenant software (SaaS) built with yii2 on the Advanced Template, but I am not having the desired result about the tenants database connection.
I am trying to set the Database Connection as next in my config file for the frontend:
$defaultAdminDB = [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
$config = [
'components' => [
'db' => function(){
if (Yii::$app->session->get('login', false)){
return [
'class' => 'yii\db\Connection',
'dsn' => Yii::$app->session->get('client_connection.dns'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
'charset' => 'utf8',
];
}
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=untitled',
'username' => 'postgres',
'password' => 'myPass',
'charset' => 'utf8',
];
},
'dbAdmin' => $defaultAdminDB
]
];
Then I have a two steps log in, where the first asks for the login (tenant id) and the next provides the user and password. On the first controller I do the next with:
$account = \frontend\models\AdminAccounts::findOne(['login'=>$this->login]);
if (!$account){
$this->addError('login', Yii::t('app', 'Account data not found.'));
return false;
}
$dns = sprintf('pgsql:host=%s;dbname=%s', $account->getAttribute('db_host'), $account->getAttribute('db'));
Yii::$app->session->set('login', $this->login);
Yii::$app->session->set('client_connection.dns', $dns);
Yii::$app->session->set('client_connection.username', $account->getAttribute('db_user'));
Yii::$app->session->set('client_connection.password', $account->getAttribute('db_pass'));
I get successfully the account (tenant) data and store it in session, but I guess my error is on instantiating the yii\db\Connection on the model's getDb() method.
Hope can help me.
Regards!
If you are changing the db connection info of an existing db connection then you should close the existing connection (Yii::$app->db->close()), change the connection info, and then open the new connection (Yii::$app->db->open()).
You can try
Yii::$app->db = new \yii\db\Connection([
'dsn' => Yii::$app->session->get('client_connection.dsn'),
'username' => Yii::$app->session->get('client_connection.username'),
'password' => Yii::$app->session->get('client_connection.password'),
]);

CakePHP 3 Auth Logout-Error: Authentication adapter "loginAction" was not found

For some reason the CakePHP Auth Component won't let me log out.
Strangely this error occurred only recently and I can't remember changing anything at the Auth Component at my CakePHP 3 app.
Im setting up the Auth component the following way:
$this->loadComponent('Auth', [
'authenticate' => [
'loginAction' => [
'controller' => 'User',
'action' => 'login'
],
'Form' => [
'finder' => 'auth',
'fields' => ['username' => 'email', 'password' => 'password']
],
'storage' => 'Session'
]
]);
$this->Auth->allow();
Login works as expected, but when I logout with $this->Auth->logout() I get the following error:
An Internal Error Has Occurred
Error: Authentication adapter "loginAction" was not found.
Then, when I remove the array 'loginAction' from the Auth Component completely the error changes to Error: Authentication adapter "storage" was not found.
It seems to work though, when I load the component without any params:$this->loadComponent('Auth')
For the login I need my Auth component set up like described above. Its similar like CakePHP does it in its docs: http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers
Any Ideas why CakePHP wont let me logout anymore all of a sudden?
Oh my! I cant beleave I wasted all those hours...
The Auth params are messed up, thats how they should look like:
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'User',
'action' => 'login'
],
'authenticate' => [
'Form' => [
'finder' => 'auth',
'fields' => ['username' => 'email', 'password' => 'password']
],
],
'storage' => 'Session'
]);

Categories