Google API Client and Cronjob

I use the Google API client to read emails from Gmail. Now I want to add Cronjob so that it reads emails every 5 minutes.

The problem with using the Google API Client is that it must allow the user to click the authorization link first and allow the user to use the Google API Client.

I have an Inbox class with initializing a function that initializes the Google API Client. But cronjob does not work, because I need to get access_token.

public function initialize() { $configuration = Configuration::getConfiguration('class_Inbox'); // Creates the Google Client $this->client = new Google_Client(); $this->client->setApplicationName('Tiptsernetwork'); $this->client->setClientId($configuration['clientId']); $this->client->setClientSecret($configuration['clientSecret']); $this->client->setRedirectUri('http://www.tipsternetwork.nl/cronjob/authenticate'); $this->client->addScope('https://mail.google.com/'); $this->client->setApprovalPrompt('force'); $this->client->setAccessType('offline'); // Creates the Google Gmail Service $this->service = new Google_Service_Gmail($this->client); // Authenticates the user. if (isset($_GET['code'])) { $this->authenticate($_GET['code']); } // Check if we have an access token in the session if (isset($_SESSION['access_token'])) { $this->client->setAccessToken($_SESSION['access_token']); } else { $loginUrl = $this->client->createAuthUrl(); echo '<a href="'.$loginUrl.'">Click Here</a>'; } // If the token is expired it used the refresh token to generate a new Access Token if($this->client->isAccessTokenExpired()) { $this->client->refreshToken($configuration['refreshToken']); } } public function authenticate($code) { // Creates and sets the Google Authorization Code $this->client->authenticate($code); $_SESSION['access_token'] = $this->client->getAccessToken(); $this->client->refreshToken($configuration['refreshToken']); // Redirect the user back after authorizaton $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($url, FILTER_VALIDATE_URL)); } 

Do you guys know how to fix this using an update token or something else? I can't get it to work, and I have no ideas.

If I access the URL and click "Click Here" and allow it to work successfully, but not with Cronjob, because I can not click on the link "Click here" ...

I hope you people understand this and can help me :).

Yours faithfully,
Yanik

+5
source share
1 answer

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

+3
source

Source: https://habr.com/ru/post/1236684/


All Articles