This extensions provides a wrapper to google-api-php-client library providing an easy way to access google apis and authentication. The extension is intended to enable any web application to access the Google Api with a service account or requiring the user authentication.
Requirements ¶
Yii 1.1.8 PHP >= 5.3
Instalation ¶
Copy
- Copy files under extension folder
Configuration ¶
- config/main.php
// application components
'components' => array(
'JGoogleAPI' => array(
'class' => 'ext.JGoogleAPI.JGoogleAPI',
//Default authentication type to be used by the extension
'defaultAuthenticationType'=>'serviceAPI',
//Account type Authentication data
'serviceAPI' => array(
'clientId' => 'YOUR_SERVICE_ACCOUNT_CLIENT_ID',
'clientEmail' => 'YOUR_SERVICE_ACCOUNT_CLIENT_EMAIL',
'keyFilePath' => 'THE_PATH_TO_YOUR_KEY_FILE',
),
/*
//You can define one of the authentication types or both (for a Service Account or Web Application Account)
webAppAPI = array(
'clientId' => 'YOUR_WEB_APPLICATION_CLIENT_ID',
'clientEmail' => 'YOUR_WEB_APPLICATION_CLIENT_EMAIL',
'clientSecret' => 'YOUR_WEB_APPLICATION_CLIENT_SECRET',
'redirectUri' => 'YOUR_WEB_APPLICATION_REDIRECT_URI',
'javascriptOrigins' => 'YOUR_WEB_APPLICATION_JAVASCRIPT_ORIGINS',
),
*/
'simpleApiKey' => 'YOUR_SIMPLE_API_KEY',
//Scopes needed to access the API data defined by authentication type
'scopes' => array(
'serviceAPI' => array(
'drive' => array(
'https://www.googleapis.com/auth/drive.file',
),
),
'webappAPI' => array(
'drive' => array(
'https://www.googleapis.com/auth/drive.file',
),
),
),
//Use objects when retriving data from api if true or an array if false
'useObjects'=>true,
),
...
),
Usage ¶
- Create a service
$service = Yii::app()->JGoogleAPI->getService('Drive');
or
$service = Yii::app()->JGoogleAPI->getService('Drive','webappAPI');
//if the authentication type is diferent of the default
- Create a object
$file = Yii::app()->JGoogleAPI->getObject('DriveFile','Drive');
//we pass the object name that we want to create and the service where it belongs
//or
$file = Yii::app()->JGoogleAPI->getObject('DriveFile','Drive','webappAPI');
//if the authentication type is different of the default
If you choose 'webappAPI' authentication method you need aditional steps, because you need to save the token from
the authenticate method
API Ex: http://code.google.com/p/google-api-php-client/
Ex:
//Create an extension Instance
$jgoogleapi = Yii::app()->JGoogleAPI;
try {
if(!isset(Yii::app()->session['auth_token'])) {
//Get the instance of the client from the api
$client = $jgoogleapi->getClient();
//or
//$client = Yii::app()->JGoogleAPI->getClient(); #Without creating an extension instance
//Web Application User authentication
//You want to use a persistence layer like the DB or memcached to store the token for the current user
$client->authenticate();
//or
//$jgoogleapi->getClient()->authenticate();
//or
//Yii::app()->JGoogleAPI->getClient()->authenticate();
Yii::app()->session['auth_token']=$client->getAccessToken();
} else {
$client->setAccessToken(Yii::app()->session['auth_token']);
//List files from Google Drive
$files = $jgoogleapi->getService('Drive')->files->listFiles();
//Check the api documentation to see other ways to interact with api
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
Yii::app()->session['auth_token'] = $client->getAccessToken();
}
}catch(Exception $exc) {
//Becarefull because the Exception you catch may not be from invalid token
Yii::app()->session['auth_token']=null;
throw $exc;
}
Resources ¶
This extension uses google-api-php-lib-0.6.2
Note ¶
When upgrading this extension check the documentation of Google API, the updated version works like the previkous one, but the google-api-client-php has some diferences from previous version.
Google analytics?
Hello, is this useful for displaying Google Analytics data on our platform dashboard? Could you do an example?
thanks in advance
Google Analytics
Hello, this extension is useful to work with the Google API, this is a wrapper to the google-api-php-lib-client, and it can do anything that lib does, i'm almost sure it can show the information that you need from analytics, i just don't know how you can do it because i don't have explored the analytics api, but the authentication process is just like in the example. You can see this Analytics Example for one example of implementation and check the Analytics Api Documentation, they also have Hello Analytics API Tutorial where you can see more one example. Just don't forget to define the right scopes that you need to use, you can find it in the api documentaion.
Sorry for not giving one example but i really don't have time now to investigate the analytics api.
Hope the information helps, and please let me know if you need some assistance with the extension.
RE: Google analytics?
@aleksdj very esy
config
'scopes'=> array( 'serviceAPI'=>array( 'analytics'=>array( 'https://www.googleapis.com/auth/analytics.readonly', ), ), ),
Use analytics api
$service = Yii::app()->JGoogleAPI->getService('analytics'); $results = $service->data_ga->get( 'ga:XXXXXXXX', '2013-05-20', '2013-05-27', 'ga:visits', array( 'dimensions' => 'ga:source,ga:date', 'sort' => 'ga:date,-ga:visits', 'filters' => 'ga:visits>100', 'max-results' => '100' ) ); // you can use Google Analytics Query Explorer 2: http://ga-dev-tools.appspot.com/explorer/
Yes you can display analytics data on dashboard
First configure the extension, I'm just putting a example config file used in a project:
<?php /* * How to obtain a Service Account: * https://developers.google.com/accounts/docs/OAuth2ServiceAccount * * * (403) User does not have any Google Analytics account. * http://stackoverflow.com/a/13167988/115050 * * */ return array( 'class' => 'ext.JGoogleAPI.JGoogleAPI', //Default authentication type to be used by the extension 'defaultAuthenticationType'=>'serviceAPI', //Account type Authentication data 'serviceAPI' => array( 'clientId' => '...', 'clientEmail' => '...', 'keyFilePath' => dirname(__FILE__).'/../extensions/JGoogleAPI/keys/Analytics-a0e8e345f273.p12', ), /* //You can define one of the authentication types or both (for a Service Account or Web Application Account) webAppAPI = array( 'clientId' => 'YOUR_WEB_APPLICATION_CLIENT_ID', 'clientEmail' => 'YOUR_WEB_APPLICATION_CLIENT_EMAIL', 'clientSecret' => 'YOUR_WEB_APPLICATION_CLIENT_SECRET', 'redirectUri' => 'YOUR_WEB_APPLICATION_REDIRECT_URI', 'javascriptOrigins' => 'YOUR_WEB_APPLICATION_JAVASCRIPT_ORIGINS', ), */ 'simpleApiKey' => 'AIzaSyAx63Ht-0XmuLdp0-j9zVREKNsCyqXgeUA', //Scopes needed to access the API data defined by authentication type 'scopes' => array( 'serviceAPI' => array( 'drive' => array( 'https://www.googleapis.com/auth/drive.file', ), 'Analytics'=>array( 'https://www.googleapis.com/auth/analytics.readonly', ), ), 'webappAPI' => array( 'drive' => array( 'https://www.googleapis.com/auth/drive.file', ), ), ), //Use objects when retriving data from api if true or an array if false 'useObjects'=>false, );
Now how I'm using it:
$profileId='123'; // Google Analytics website ID $api = Yii::app()->JGoogleAPI->getService('Analytics'); $api->data_ga->get('ga:' . $profileId, // Dates '2013-01-01', '2013-12-31', //Metrics 'ga:itemRevenue,ga:itemQuantity', array( //Dimensions 'dimensions' => 'ga:productName', 'sort' => '-ga:itemRevenue', // 'filters' => 'ga:medium==organic', 'max-results' => '10'));
Not tested this lately so you might have to adjust some things.
The rest is reading google analytics api docs.
Keep in mind that is important to register your google application and get a service account.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.