I am having a really weird issue with the Google Admin SDK (I am using the Google API PHP client library). I can succesfully add groups and users and generally use the API on my local development machine, but when I put it on the live website it crashes with this error:
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }''
I am using it in combination with a service account. For this I generated a p12 key, and a client JSON file which I both included in the project. I also use a real account as a delegated admin account.
I am using the following code to connect to the Google SDK:
<?php
$this->client = new Google_Client();
$this->client->setAuthConfigFile(STORAGE_PATH.'client_secrets.json');
$this->client->addScope(static::$scopes);
$cred = new Google_Auth_AssertionCredentials(
static::$service_account_email,
static::$scopes,
file_get_contents(STORAGE_PATH.'TRICS-key.p12'));
$cred->sub = static::$delegated_admin;
$this->client->setAssertionCredentials($cred);
$this->directory_service = new Google_Service_Directory($this->client);
Does someone know what could be wrong?
Mystery solved. It appeared that our live environment had it's time 5 minutes in the future. This caused problems with the OAuth2 authentication. So, if you have the same problem, make sure that your time is set properly, preferably automatically via NTP!