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.
Yii 1.1.8 PHP >= 5.3
Copy
// 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, ), ... ),
$service = Yii::app()->JGoogleAPI->getService('Drive'); or $service = Yii::app()->JGoogleAPI->getService('Drive','webappAPI'); //if the authentication type is diferent of the default
$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; }
This extension uses google-api-php-lib-0.6
Total 2 comments
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.
Hello, is this useful for displaying Google Analytics data on our platform dashboard? Could you do an example? thanks in advance
Leave a comment
Please login to leave your comment.