Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
BlogTech

Google Client API with PHP – step by step tutorial with (some) screenshots

By techsupport
December 3, 2014 4 Min Read
Comments Off on Google Client API with PHP – step by step tutorial with (some) screenshots

Google Client API with PHP – step by step tutorial with (some) screenshots | enarion.net

Google Client API with PHP – step by step tutorial with (some) screenshots

This page describes how you can access Google data with some handy PHP code, using the official Google Client API for PHP and OAuth2. To make it short, this is not a dirty hack but provides the official way of doing this. Unfortunately the samples provided in the client library by Google might not suite you very well. This script is the foundation for the other Google client API tutorials.

Info: in case you use composer to manage your dependencies, have a look at google/apiclient at github.

Goal

Create and setup up an Google API console project and finally connect with a simple PHP script.

Step 1: Setup the Google API project

Before you can access the API, you need to set it up.
  1. Go the the Google API console.
  2. Select Services and check Analytics API:
  3. Select API Access. You should see something like the following:
  4. Click on “Create an OAuth 2.0 client ID”. Write something into Product name. Check the value for Google account – this should be your (correct) Google account.
  5. Click on Next. Now you select “Web application” (since PHP is mostly used for web applications and this tutorial is no exception). Use localhost if you would like to try it out, otherwise you can also use a domain.
  6. Click on (more options) and change the value for Authorized Redirect URIs as in the following screenshot (remove oauth2callback). You can change this list later if more than one url are required. Finally click on “Create client ID”.
  7. Now your account is created. You should see something like this:
The important values are ClientID, Client secret, Redirect URIs and the API key.

Step 2: calling Google

Now it’s time to connect with the client API to Google. OAuth2 is used as protocol in the background. You don’t need the details, but it’s good to know. RTFM
Please do the following steps now:
  • Download the latest release of the Google Client API PHP library and store it on your webserver. Rename it to GoogleClientApi.
  • Create an empty file (e.g. googleapitest.php) and store it on your webserver. We assume that it is available on the url www.yourdomain.com/googleapitest.php . You can use every name and url you like, just adapt the path in the following steps accordingly.
  • Check that the url of the test script (www.yourdomain.com/googleapitest.php) is in the list of Redirect URIs in your Google API console. (see number 6 and 7 in step 1)
  • Copy the following code into the test script:
<?php
session_start();
require_once dirname(__FILE__).'/GoogleClientApi/Google_Client.php';
require_once dirname(__FILE__).'/GoogleClientApi/contrib/Google_AnalyticsService.php';

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('My Application name');
$client->setClientId('INSERT HERE');
$client->setClientSecret('INSERT HERE');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('INSERT HERE'); // API key

// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
	die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
echo 'Hello, world.';

step 3: try it

Now start the test script in your browser, e.g. www.yourdomain.com/googleapitest.php.

You will be redirected to Google for the authentification of the access of your web app to your Google profile data. If you are already logged in with a Google profile, the following screen will appear:

(You will see the message and buttons in the language of your browser)

Press acknowledge (left button) and you will be redirected to your test script. Since no functionality is implemented, you will only see “Hello, world”.

Now you can continue with the next article Accessing Google Analytics with Google Client API and PHP.

Further reading

  • Accessing Google Analytics with Google Client API and PHP
  • Google Client API PHP library
  • Google OAuth for webserver
  • Good tutorial for accessing Google Analytics API data
  • Download sample code
Author

techsupport

Follow Me
Other Articles
Previous

오픈 그룹웨어 정리

Next

jQuery 레이어 팝업 라이브러리

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme