Introduction
This article describe how to access Google data with some PHP code using the official Google Client API for PHP and OAuth2. To do that use the following steps.
Step 1
The Analytics API looks like:
Step 2
After the completion of Step 1, select "API Access", which looks like:
Step 3
Then click on "Create an OAuth 2.0 client ID", write something about the Product Name, and then write something like:
Step 4
In Step 4, click on the "Next" button of the image above, select The Application Type as a Web application and provide the a site or host name; you can use localhost as a host name or your site's domain.
Step 5
In Step 5, click on "More options", and you can change the value of Authorized Redirect URLs. You can also change it later if more than one URLs will be required and then click on the "Create Client ID" button.
Step 6
After completing all these steps, the account has been created. The follwing image shows how it looks like:
Step 7
Now it's time to connect with the client API to Google. OAuth2 is used as the protocol in the background. So now:
DownLoad The Google Client API Library
After the downloading, extract the downloaded file, and paste it in your "webserver" root and you can also change the name. I named it "Myapi". Suppose I am using a wamp server, so I use (in other words download the file) into this "C:\wamp\www" address.
Step 8
Create an empty file (e.g. "googleapitest.php") and store it on your webserver. We assume that it is available at the URL "localhost/googleapitest.php". You can use whatever name and URL you like, just adapt the path in the following steps accordingly.
Check your Redirect in your google API console (see step 6).
Then copy the following code into the "googleapitest.php" file:
<?php
session_start();
require_once dirname(__FILE__).'\Myapi\src\Google_Client.php';
require_once dirname(__FILE__).'\Myapi\src\contrib\Google_PlusService.php';
//session_start();
$client = new Google_Client();
$client->setApplicationName('Myapi');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
//$url = "http://localhost:9080/oauth2callback";
$client->setClientId('insert client code here');//insert client code here
$client->setClientSecret('insert secrete code here ');//insert secrate code here
$client->setRedirectUri('http://localhost:9080/oauth2callback'); //insert redirect url here
$client->setDeveloperKey('insert API key');// insert api key here
$plus = new Google_PlusService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}
?>
Step 9
Now run your script, the output looks like:
Step 10
After the completion of step 9, click on the "Connect Me!" link. You will see the output like in the following image:
Now you can add your own functionality.