Google Service Account With PHP Client

In our Google Service Account tutorial I showed you how to setup a service account and the proper scopes.
I will now show you how to use the service account key within your PHP application.

Step 1
Get a copy of the Google API PHP Client and upload to your server.

Step 2
Include the autoload.php

require_once '__PATH_TO__/vendor/autoload.php';// Replace __PATH_TO__ with your actual path

Step 3
Upload the previous service-account.json file outside your public html folder.

Step 4
Add the follow codes to use the service account key and the api client

      $client = new Google_Client();

      $server_root = str_replace("/public_html", "", $_SERVER['DOCUMENT_ROOT']);

      putenv('GOOGLE_APPLICATION_CREDENTIALS='.$server_root.'/service-account.json');

      $client->useApplicationDefaultCredentials();
      $client->setApplicationName("My Calendar App");

      $client->setScopes(['https://www.googleapis.com/auth/calendar']);

      // Start service
      $service = new Google_Service_Calendar($client);

You can now use $service to make your api calls including updating the Google calendar, creating Google calendar and more.

That’s it. I hope you enjoyed the tutorial, leave a comment and follow us on Twitter @westernstudios.

We do custom Google Apps integration with our fee $35/hr. Contact us for service.


  1. Jimmy says:

    I have been browsing online more than 3 hours today,
    yet I never found any interesting article like yours.

    It’s pretty worth enough for me. In my view, if all webmasters and
    bloggers made good content as you did, the net will be a
    lot more useful than ever before.

  2. borvest inkral says:

    I always was interested in this topic and stock still am, regards for posting.

Please Post Your Comments & Reviews

Welcome to Western Studios

We provide professional website and mobile softwares including Joomla, Wordpress and more.