picqer / moneybird-php-client

PHP Client for Moneybird V2
MIT License
82 stars 77 forks source link

401 when using the example #188

Closed guillome2k closed 3 years ago

guillome2k commented 5 years ago

Hi, when i use the example code and a i got this error. After refresh or sleep of at least 2 seconds its all good.

1 little change in the code is:


$moneybird = new \Picqer\Financials\Moneybird\Moneybird($connection);
print_r($moneybird->administration()->getAll());exit;
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://moneybird.com/api/v2/administrations.json` resulted in a `401 Unauthorized` response: {"error":"token is invalid","symbolic":{"request":"invalid_grant"}} in ...\thirdparty\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 ...\thirdparty\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 ...\thirdparty\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 ...\thirdparty\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) #3 ...\thirdparty\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::Guz in ...\thirdparty\vendor\picqer\moneybird-php-client\src\Picqer\Financials\Moneybird\Connection.php on line 500

What is going wrong?

dennisameling commented 5 years ago

@guillome2k Can you give some more details about your implementation? The code snippet you provided is not enough for us to help troubleshoot.

guillome2k commented 5 years ago

Hi Dennis, Thanks for your reaction.

I am using OAuth2 with callback for external application, and completely using this code: https://github.com/picqer/moneybird-php-client/tree/master/example With only 1 change i mentioned in my first post, because i can not hard code the administration ID as you do in your example.

And your second point is correct, after authorize i need to wait for 2 seconds to make a call like /administrations.

dennisameling commented 5 years ago

I just tested on my end, and it looks like it's an issue on Moneybird's side. I got the same error as you got:

Picqer\Financials\Moneybird\Exceptions\ApiException : Error 401: {"error":"token is invalid","symbolic":{"request":"invalid_grant"}}

I then tried with Moneybird's own CURL example, where the exact same happened. Like you said, if you wait for a few seconds before issuing an API call, everything works fine. It looks like Moneybird's backend needs to do some processing after issuing a token, which doesn't happen in time for you requesting some API data.

Would you mind reporting this issue to Moneybird and keeping us posted on the status? Feel free to reference this GitHub issue. Thanks in advance!

guillome2k commented 5 years ago

I`ll do that. But 1 addition: the Dropbox OAUth2 connection has exact the same problem. Maybe it is an general OAuth2 problem.

dennisameling commented 5 years ago

What I've seen before, is that the time server of external parties is a few seconds off your own server's time (like when Moneybird's time is 14:01:05 and your time is 14:01:00, then Moneybird's token will not be valid on your system yet). This is just some speculation; it's best to leave it up to Moneybird to get this fixed ;-)