PHP code example of happyr / google-api-bundle

1. Go to this page and download the library: Download happyr/google-api-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

happyr / google-api-bundle example snippets




// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new HappyR\Google\ApiBundle\HappyRGoogleApiBundle(),
    // ...
);



namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

class GoogleOAuthController extends Controller
{
  /**
   * @Route("/oauth/google/auth")
   */
  public function getAuthenticationCodeAction()
  {
  }

  /**
   * @Route("/oauth/google/redirect")
   */
  public function getAccessCodeRedirectAction(Request $request)
  {
  }
}

// ...

  private $accessScope = [
    \Google_Service_Calendar::CALENDAR
  ];

  /**
   * @Route("/oauth/google/auth")
   */
  public function getAuthenticationCodeAction()
  {
    $client = $this->container->get('happyr.google.api.client');

    // Determine the level of access your application needs
    $client->getGoogleClient()->setScopes($this->accessScope);

    // Send the user to complete their part of the OAuth
    return $this->redirect($client->createAuthUrl());
  }

 // ...
 

// ...

  private $accessScope = [
    \Google_Service_Calendar::CALENDAR
  ];

// ...

  /**
   * @Route("/oauth/google/redirect")
   */
  public function getAccessCodeRedirectAction(Request $request)
  {
    if($request->query->get('code'))
    {
      $code = $request->query->get('code');

      $client = $this->container->get('happyr.google.api.client');
      $client->getGoogleClient()->setScopes($this->accessScope);
      $client->authenticate($code);

      $accessToken = $client->getGoogleClient()->getAccessToken();

      // TODO - Store the token, etc...
    } else {
      $error = $request->query->get('error');
      // TODO - Handle the error
    }
  }

// ...
bash
$ php composer.phar update