How to Google-Authentication OAuth2 into an account with given login-data?

Viewed 109

My aim is it to write a php script that query all events from an existing google calendar of one specific google account.

The default scripts to do this works fine. At the moment it works so: Request my php script --> it recognizes that is has no $_GET['code'] parameter --> redirect the browser to google for login --> after login google redirects back to my php script --> and the query on the calendar can be done

But what i want to do now: is that my php script performs the login into this specific google account with the username and password. I have and my script shell retrieve the $_GET['code'] parameter without any user interaction.

I have already spent lots of hours researching and testing but i have not found any solution yet.

Is that even possible? If it is there a way to pass these login datails (username and password) within the request to google authendication to the two-step login form?

In this thread is an hint to use service-accounts: Fatal error: Class 'Google_Auth_AssertionCredentials' not found but i have not figured out how to do this.

Thanks in advance.

2 Answers

Logging in to a google account using username and password i was called client login. Google shut this down in 2015.

This is no longer an option. If you want to access private user data you need to use Oauth2 and request authorization as you are doing currently.

service accounts

Note: to clarify @CBroe statement in comments.

Service accounts only work with google calendar if you are using a google workspace domain accounts. You would need to configure domain wide delegation and set up the service account to impersonate a user on the domain.

For standard Gmail user accounts service accounts are not possible with google calendar api.

Finally i made it with a payd google workspace subscription.

At the moment i am not able to reconstruct all the steps but i can give a few hints and ressources that helped me.

Here is an official example for the usage of a service account: https://github.com/googleapis/google-api-php-client/blob/main/examples/service-account.php The templates/base.php file was not part of my composer installation so i had to deploy it myself. It also was necessary to adjust the path to my service-account-credentials-json-file.

Here is another thread about the same topic with some really good explanations: Inserting Google Calendar Entries with Service Account

And then only thing that was missing in my configuration: In the calendar settings i had to add the service accound id (with the @ in it) to grant access to this service account.

For better understanding of the backgrounds i also refer to the comments in this thread.

Another hint: In the google workspace account i had to validate my domain and recreate the project with its credentials and service account.

Related