Invalid user authorisation after a period

Using: access_token and client_id to setup connection to API after authorisation.
Sending a announcement chat to user, this is the result payload and sending chat works fine:

Array
(
[status] => ok
[message] =>
[api_data] =>
[endpoint] => https://api.twitch.tv/helix/chat/announcements?broadcaster_id=***&moderator_id=***
[url_params] => Array
(
[broadcaster_id] => ***
[moderator_id] => ***
)

[payload] => {"message":"Heyoooo - This is a test message! ROGER!: 2022-09-15 12:29:29", "color":"green"}

)

But, sometimes after a period, I get a response payload that user is unauthorised (I guess the sender is).

The strange thing is that I use a ‘users’ request to check if token is valid by pulling a user object from twitch does not give a 401, but responds with another user object, not the sender (bearer only). It seems that the access token is valid but dont have rights to go on. Chat not sent → unauthorised.

I need to login to twitch again, refresh tokens en then the chat works again.

What could be the issue?

Whats the exact body response?

What is the request you are making?

If you call /users with no ID/Login then it will return the user for the token specified.

So if you are only using user tokens and getting the wrong user to your /users call, sounds like you are mis handling your user tokens and getting cross talk between your processes as the script is using the wrong user token.

Sounds like someone else is logging into your app and the keys being loaded into the wrong bot/script

Got it running, it was a typo in the refresh token method. Thanks @BarryCarlyon