Yii 1.1: jgoogleapi

JGoogleAPI it's an Yii extension to use the Google Api PHP Client in a easy way.
16 followers

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.

Total 3 comments

#13376 report it
mishamx at 2013/05/26 11:36am
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/
#10941 report it
n3okill at 2012/12/05 10:52am
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.

#10939 report it
aleksdj at 2012/12/05 08:23am
Google analytics?

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 to leave your comment.

Create extension