0

I try to connect to my Google Calendar using the Google API for PHP, version 2.2.0. Until now I do not succeed in connecting to my calendar and that is very frustrating.

At this moment I receive the following fatal error:

Fatal error: Uncaught InvalidArgumentException: json key is missing the type field in /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php:123 Stack trace: #0 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/ApplicationDefaultCredentials.php(154): Google\Auth\CredentialsLoader::makeCredentials('https://www.goo...', Array) #1 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(1078): Google\Auth\ApplicationDefaultCredentials::getCredentials('https://www.goo...') #2 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(365): Google_Client->createApplicationDefaultCredentials() #3 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(786): Google_Client->authorize() #4 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Service/Resource.php(232): Google_C in /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php on line 123

I use this code:

require_once WP_PLUGIN_DIR . '/google-api-php-client-2.2.0/vendor/autoload.php';
require_once WP_PLUGIN_DIR . '/google-api-php-client-2.2.0/src/Google/Client.php';
require_once WP_PLUGIN_DIR . "/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/Calendar/Resource/CalendarList.php";
require_once WP_PLUGIN_DIR . "/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/Oauth2.php";


putenv('GOOGLE_APPLICATION_CREDENTIALS=' . dirname(__FILE__) . '/oauth-credentials.json');

$client = new Google_Client();
$redirect_uri = 'https://myredirect-uri';
$client->setRedirectUri($redirect_uri);

$client->setRedirectUri($redirect_uri);
$client->setClientId('myclient-di.apps.googleusercontent.com');
$client->setClientSecret('myclient-secret');
$client->setAccessType("offline");        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth
$client->addScope(Google_Service_Calendar::CALENDAR);

$user_to_impersonate = 'owner-of-agenda@gmail.com';
$client->setSubject($user_to_impersonate);
$client->setAuthConfig(GOOGLE_APPLICATION_CREDENTIALS);

$client->useApplicationDefaultCredentials();

if (file_exists(CREDENTIALS_PATH)) {
    $token = file_get_contents(CREDENTIALS_PATH);
    echo "<h4>_Token</h4>";
    var_dump($token);
    $client->setAccessToken($token);
}
echo "<br><br>CLIENT";

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();
var_dump($calendarList);

echo "<h4>End of example</h4>";

I see that an token is generated.

Could someone please assist me in making this connection?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leon N
  • 103
  • 2
  • 14
  • Looks to me like the json blob in `/oauth-credentials.json` is missing a `type` field. Since you didn't include that file's content, it is hard to debug, but I would start by comparing that to what Google expects to find in it. – Alexander Morland Oct 19 '17 at 12:17
  • The content of the file is directly from google, so I would think it should be okay. Could be that I did not configure the settings not okay ..... pffftt – Leon N Oct 19 '17 at 12:29
  • Sorry, content of the file: {"web":{"client_id":"my-client-id","project_id":"my-project-id","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"my-client-secret","redirect_uris":["https://my-redirect-uri"],"javascript_origins":["https://my-website.nl"]}} – Leon N Oct 19 '17 at 12:29
  • I solved it finally; the json-key-file did not contain the type field. But I removed some statements and went back to the minimum of data. Added also the statement: $client->setApprovalPrompt('force'); – Leon N Oct 22 '17 at 21:35
  • @LeonN what you did is very unclear as solution because of "some statements and went back to the minimum of data." Very vague description of what you did to solve. – m3nda Jun 27 '23 at 10:39

3 Answers3

1

I solved it, The JSON-key-file did not contain the type field. But I removed some statements and went back to the minimum of data. Added also the statement:

$client->setApprovalPrompt('force');

Leon N
  • 103
  • 2
  • 14
  • Maybe this is old and doesn't apply anymore ? I tried with this but still getting the same error. – m3nda Jun 27 '23 at 10:40
1

There was something you did not see. There is a dropdown Create Credentials click on that and you will see three options:

  1. API Key
  2. OAuth Client ID
  3. Service Account Key

Choose service account key, select your project name and download the json file from there, put that json file in here, putenv('GOOGLE_APPLICATION_CREDENTIALS=SellerWizard.json');

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
0

I've got crazy today with this.

Case of mine, it was an older PHP client api for PHP7, which used to find client_id and secret_id in the file tokens.json.

It was not accepted by setClientId() methods, i mean, where accepted, but not used in the operations.

The solution for me has been to add both client_id and client_secret inside tokens.json.

This did not happend whe using other versions of the api.

m3nda
  • 1,986
  • 3
  • 32
  • 45