infusionsoft / infusionsoft-php

PHP client library for the Infusionsoft API.
https://developer.infusionsoft.com/
Other
129 stars 126 forks source link

401 Unauthorized Even Token Not Expired #273

Closed arivazhaganark closed 3 years ago

arivazhaganark commented 3 years ago

When authorise Infusionsoft via OAuth2, the response token expire with in few hours

$token->getEndOfLife() --> 1612436406 --> 2021-02-04 06:00:06

But debug the \Infusionsoft\Token Object,

Infusionsoft\Token {#2982
  +accessToken: "q13792KCt3AUek8w4G5b1pAm1o3L"
  +refreshToken: "NTNL4PZueWWcPpqKb8rHPqK8rM8lAb6r"
  +endOfLife: 3224018766
  +extraInfo: array:2 [
    "token_type" => "bearer"
    "scope" => "full|fc830.infusionsoft.com"
  ]
}

$token->getEndOfLife() --> 3224018766 --> 2072-03-09 05:07:48

Even after expired (2021-02-04 06:00:06) the method $this->isTokenExpired() still comes TRUE

So the API returns 401 UnAuthorized

Ultimater commented 3 years ago

When newly authorized, it should last for 24 hours. The endOfLife returned from the API should be the seconds from when it was authorized until it expires. The API doesn't return a Unix timestamp for when it expires. Thus if you're going to rely on such information, need to also store the timestamp it's relative to.

In my JavaScript console when I run:

(new Date(1612436406*1000)).toUTCString()

I see

Thu, 04 Feb 2021 11:00:06 GMT

In order to reach a date like 2072, this would look like the same leap from the 1970 Epoch. Thus it looks like you're adding it twice:

(new Date(1612436406*2*1000)).toUTCString()

Thu, 10 Mar 2072 22:00:12 GMT

This value is within about 7 hours of the date you posted.

Looks like it's getting added twice somehow. Should investigate what's going on each step of the way until you understand what your code is doing.

To avoid these kind of issues, I wouldn't rely entirely on the end of life token.. I'd still have a cronjob to refresh the token. At least that's what I do. I believe the system performs better with such a cronjob. Doing that sort of extra work when there's an actual user request you're handling just increases the chance of network issues and adds to the wait, etc. I don't rely entirely on cronjobs either. I currently use a mix of the end of life token and a cronjob to keep the database tokens fresh.

But yeah, looks like you're adding something twice. Should definitely look what's going on before getting into cronjobs.

arivazhaganark commented 3 years ago

Ah yes, i was fixed that. My Token was serialize twice