A symfony2 bundle to communicate to Google API. This bundle is a Symfony2 wrapper for the google apiclient. There are some services not yet implemented. Please submit a PR and I'm happy to merge.
Install it with Composer!
// composer.json{// ...require: {// ..."happyr/google-api-bundle": "~2.1",}}Then, you can install the new dependencies by running Composer's update
command from the directory where your composer.json file is located:
$ php composer.phar updateTo register the bundles with your kernel:
<?php// in AppKernel::registerBundles()$bundles = array(
// ...newHappyR\Google\ApiBundle\HappyRGoogleApiBundle(),
// ...
);# app/config/config.yml# you will get these parameters form https://code.google.com/apis/console/"happy_r_google_api:
application_name: MySiteoauth2_client_id:
oauth2_client_secret:
oauth2_redirect_uri:
developer_key:
site_name: mysite.comCreate a controller with authenticate and redirect methods.
<?phpnamespaceAppBundle\Controller;
useSymfony\Bundle\FrameworkBundle\Controller\Controller;
useSensio\Bundle\FrameworkExtraBundle\Configuration\Route;
useSymfony\Component\HttpFoundation\Request;
class GoogleOAuthController extends Controller
{
/** * @Route("/oauth/google/auth") */publicfunctiongetAuthenticationCodeAction()
{
}
/** * @Route("/oauth/google/redirect") */publicfunctiongetAccessCodeRedirectAction(Request$request)
{
}
}Setup the required scope of your app and redirect the user to complete their part of the OAuth request.
// ...private$accessScope = [
\Google_Service_Calendar::CALENDAR
];
/** * @Route("/oauth/google/auth") */publicfunctiongetAuthenticationCodeAction()
{
$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 OAuthreturn$this->redirect($client->createAuthUrl());
}
// ...Determine if an access code has been returned. If there is an access code then exchange this for an access token by using the client authenticate method.
// ...private$accessScope = [
\Google_Service_Calendar::CALENDAR
];
// .../** * @Route("/oauth/google/redirect") */publicfunctiongetAccessCodeRedirectAction(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
}
}
// ...If successful the response should include access_token, expires_in, token_type, and created.