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

  1. Requirements
  2. Instalation
  3. Configuration
  4. Usage
  5. Resources
  6. Note

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.
4 0
18 followers
1 389 downloads
Yii Version: 1.1
License: MIT
Category: Web Service
Developed by: n3okill
Created on: Nov 30, 2012
Last updated: 11 years ago

Downloads

show all

Related Extensions