This answer is a more general approach to using O-Auth2 Flow, as I ran into a similar problem a while ago. Hope this helps a bit.
One possible problem (if you understand the proper use of OAuth) is that you use force as a confirmation prompt. Why are you forcing the user to give permission when he has already done?
When a user self-plays against your backend, he asks if he wants to grant your permission to the actions defined in scope . The first time your application receives these permissions (by clicking the "Agree" button), your Script will receive access_token and refresh_token from google.
access_token used to access the Google API with this authenticated user account. You must save this somewhere on your server if you want to access the Google APIs without the presence of a user (called offline access). With this token, you can do anything in the username (limited to certain areas). It will go invalid after 1 hour or so. For the entire time (1 hour) you can use this token without the presence of the user!
refresh_token necessary if access_token not valid after this time period. And only then. You only get refresh_token ONCE and it will never change. This is very important data and should be kept safe!
Therefore, if you want to access the Google APIs without the presence of the user, you need to make API calls with the access_token saved. If the answer is similar to token expired (I think there was an error code for this - you need to investigate), then you call $client->refreshToken($refreshToken) with the update token that you saved somewhere safe. From this you will get a new access_token . With this access_token you can continue to work without forcing the user to click somewhere (again).
The next time the new access_token turns out to be invalid, you should use the same refresh_token as before, and the reason this refresh_token so important.
I hope I help you a little. If not, comment on this.
Happy coding
Resources
source share