exact-php-client: Could not connect to Exact: Could not acquire or refresh tokens [http 400]
I have seen some issues about this topic going around tried all possible solution but couldn’t find any solution for the problem. I can’t see what i am doing/is going wrong. This is y connect function
public function connect($request)
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl(env("EXACT_CALLBACK_URL"));
$connection->setExactClientId(env("EXACT_CLIENT_ID"));
$connection->setExactClientSecret(env("EXACT_CLIENT_SECRET"));
// Retrieves authorizationcode from database
if ($request->session()->has('authorizationcode')) {
$connection->setAuthorizationCode($request->session()->get('authorizationcode'));
}
// Retrieves accesstoken from database
if (getValue('accesstoken')) {
$connection->setAccessToken(getValue('accesstoken'));
}
// Retrieves refreshtoken from database
if (getValue('refreshtoken')) {
$connection->setRefreshToken(getValue('refreshtoken'));
}
// Retrieves expires timestamp from database
if (getValue('expires_in')) {
$connection->setTokenExpires(getValue('expires_in'));
}
// Set callback to save newly generated tokens
$connection->setTokenUpdateCallback('tokenUpdateCallback');
// Make the client connect and exxchange tokens
try {
$connection->connect();
} catch (\Exception $e) {
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}
return $connection;
}
Authorize function
public function authorize()
{
$connection = new Connection();
$connection->setRedirectUrl(env("EXACT_CALLBACK_URL"));
$connection->setExactClientId(env("EXACT_CLIENT_ID"));
$connection->setExactClientSecret(env("EXACT_CLIENT_SECRET"));
$connection->redirectForAuthorization();
}
Authorization token check
if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) {
setValue('authorizationcode', $_GET['code']);
$request->session()->put('authorizationcode',$_GET['code']);
}
// If we do not have a authorization code, authorize first to setup tokens
if (!$request->session()->has('authorizationcode')) {
$exact = new \App\Http\Controllers\ExactOnlineController();
$exact->authorize();
}
and the dump of my connection class:
-baseUrl: "https://start.exactonline.nl"
-apiUrl: "/api/v1"
-authUrl: "/api/oauth2/auth"
-tokenUrl: "/api/oauth2/token"
-exactClientId: "SECRET"
-exactClientSecret: "SECRET"
-authorizationCode: "ha1W!IAAAACBP-J0pQ9fA8Z5-KcHLG9KVPpfKFLNQlEMIqouZyAJbwQEAAAE6oJu5PSd87_F33JdNhCT_MaWKfYbZ2nHBFjy-oPOO-t9lllqIw7a-tW_627_B36xfwUkFz5dHHsSUWC_Ox9oAL6c7geEe0n5-UVi ▶"
-accessToken: null
-tokenExpires: null
-refreshToken: null
-redirectUrl: "http://127.0.0.1:8000/home"
-division: null
-client: null
-tokenUpdateCallback: "tokenUpdateCallback"
-acquireAccessTokenLockCallback: null
-acquireAccessTokenUnlockCallback: null
#middleWares: []
+nextUrl: null
#dailyLimit: null
#dailyLimitRemaining: null
#dailyLimitReset: null
#minutelyLimit: null
#minutelyLimitRemaining: null
As u can see the authorization code is filled in connect function but the access token keeps empty and is throwing this error.
Could not connect to Exact: Could not acquire or refresh tokens [http 400]
Somebody knows a solution?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 33 (4 by maintainers)
@sietse85 cool that you’ve taken the efforts to make sure it isn’t your local code. However what @remkobrenters is trying to explain we are a open source community which voluntarily provides a PHP package to interact with the Exact Online API (which is the paid service between you and Exact Online).
We can only provide you with best effort help, but are by no contract obligated. Exact knows about this package and in some occasions people have raised their problems with exact end the information we get back leads to fixes in this code. That is what I believe is the next step, you contacting Exact and trying to find the root cause en let us know about it. This would then hopefully result in the issue being fixed in this repo (again without any commitment in terms of when, how or who).
I hope this clarifies the responsibilities a bit without trying to send you of. But in short, the community needs you help.
Hi all, we have a pretty heavy used Exact implementation tool running in production in Belgium. I think the main issue with your code is the refresh token requesting.
Your code is like this:
Change this to so it refreshes 30 seconds earlier:
The reason for this is that your access token has to be valid till the end of the request. This is because Exact internally uses load balancers and they forward the request with the token to the internal API servers. They can start slightly later with your requests or have some delay processing. So basically even though your token is valid at the beginning at the request it might not be valid at the end of request and Exact API server rejects the token.
We have 0 disconnects over 1000s over companies even with concurrency.
PS if you are using Exact API in a concurrent environment make sure to add a mutex around the refresh process to prevent race conditions
Hi @sietse85 This is a community managed package. I understand that you, like a lot of developers, are having a hard time implementing the API limits imposed by Exact Online. If you have constructive tips for other developers in this community please add them to this issue. You can always contact Exact Online to tell them how you feel of course.