forrest icon indicating copy to clipboard operation
forrest copied to clipboard

No token available in 'session' storage from artisan Console

Open LunarDevelopment opened this issue 7 years ago • 9 comments

Hello,

I'm trying to implement an artisan command to get data from Salesforce.

The code works in browser with Forrest but when I run php artisan MYCOMMAND I get the following:

  No token available in 'session' storage

I can see the session tokens in my browser if I run dd(Session::all()); however running the same code in CLI gets me an empty array. I've tried every type of storage driver and I'm configuring the artisan command to use my preferred driver manually in the constructor Session::setDefaultDriver('database');

Other issues of this nature have been resolved by calling Forrest::authenticate(); but the following (overkill) constructor of my artisan command still bags me an empty session array and the above error.

    public function __construct()
    {
        Session::setDefaultDriver('database');

        Forrest::authenticate();

        parent::__construct();

        dd(Session::all());
    }
vagrant@scotchbox:/var/www/callboards$ php artisan push:salesforce
[]

LunarDevelopment avatar Aug 04 '16 12:08 LunarDevelopment

I had thought this a problem with Session not being written until the browser is used but the following works:

    public function __construct()
    {
        Session::setDefaultDriver('database');

        Forrest::authenticate();
        Session::put('k','test');

        parent::__construct();

        dd(Session::all());
    }``` 

outputs 

vagrant@scotchbox:/var/www/callboards$ php artisan push:salesforce array:1 [ "k" => "test" ]


benjacksondev avatar Aug 04 '16 13:08 benjacksondev

Resolved by using "password" instead of "webserver" authentication.

Credit to @jackson-ben

LunarDevelopment avatar Aug 07 '16 17:08 LunarDevelopment

@LunarDevelopment I know this is closed, but I am interested in hearing more about how you finally got this working, as I'm running into the exact same issue.

When I attempt to use Forrest in an all-command workflow, using the "password" authentication method, i get the same error. Seems the session is never created when in cli?

beneberle avatar Mar 07 '17 01:03 beneberle

Are you calling Forrest::authenticate(); before you run any other Forrest commands? I think this could have been the problem. Also check that you are not using browser session, i.e cookie.

benjacksondev avatar Mar 07 '17 07:03 benjacksondev

Yes, I call Forrest::authenticate(); before anything else. My app's session is configured with the 'file' driver, not cookie.

I have a job that is scheduled to run every 5 minutes. I can get it to work as long as I first visit an http route in the browser that makes a Forrest::authenticate(); call. Then I'm good to go for any cli commands for a while, at least until that browser session expires.

As a workaround, perhaps I could trigger that http request from the command itself? Or maybe I just need to reconsider my Forrest config. I'll continue to work at it and report back here any findings in case someone else goes searching.

beneberle avatar Mar 07 '17 17:03 beneberle

Have you tried Session::setDefaultDriver('file'); directly before the authenticate method?

benjacksondev avatar Mar 07 '17 18:03 benjacksondev

Getting the same error. I have a route calling index function. and the index function has this

public function index() {
	Forrest::authenticate();
	Forrest::versions();
}

I am getting the same error 'No token available in 'session' storage'

I am using the database session driver and I can see a session entry created in the sessions table.

Did anyone solve this?

UPDATE: Sorry to bother, just needed to change this: 'authentication' => 'WebServer', into this: 'authentication' => 'UserPassword', in the config/forrest.php file

LastxTemplar avatar Mar 14 '17 17:03 LastxTemplar

I'm back to this issue again, authentication is working through browser but not through artisan command.

Per the solution that worked last time my config\firrest.php is still set to 'authentication'=>'UserPassword',

  [GuzzleHttp\Exception\ClientException]
  Client error: `POST https://login.salesforce.com/services/oauth2/token` resulted in a `400 Bad Request` response:
  {"error":"invalid_grant","error_description":"authentication failure"}

LunarDevelopment avatar Mar 28 '17 09:03 LunarDevelopment

I solved this for myself by changing the config for forrest.storage.type to 'cache' instead of 'session'.

jove4015 avatar Mar 03 '18 00:03 jove4015