1. Google


Authentication

The first thing we want to do is get an client ID and client secret. This can be acheived by visiting Google's Console and creating a new app or using an existing one. We can next plug in both of those to start an authentication process as in Figure 1.

Figure 1. Instantiate Auth
$auth = eden('google')->auth('[CLIENT_ID]', '[CLIENT_SECRET]', 'http://yourwebsite.com/auth', '[API_KEY]');

With this auth class let's make sure we can generate a login URL. Figure 2 is used to redirect the user to google to continue the authentication. You need to replace http://yourwebsite.com/auth with the URL of your website and page you will be handling the Google authentication and you must indicate what scope are you going to use.

Figure 2. Get Login URL
$login = $auth->getLoginUrl($scope);

When the user has authorized our app to use their data, Google will add an auth code, we can use to transfer to an access token. Figure 3 shows how we can convert that auth code to a token.

Figure 3. Get Access Token
$access = $auth->getAccess($_GET['code']);
$_SESSION['token'] = $access['access_token'];

An example of the whole entire authentication process can be found in Figure 4.

Figure 4. Authentication
session_start();

$auth = eden('google')->auth('[CLIENT_ID]', '[CLIENT_SECRET]', 'http://yourwebsite.com/auth', '[API_KEY]');

//if no code and no session
if(!isset($_GET['code']) && !isset($_SESSION['token'])) {
    //redirect to login
    $login = $auth->getLoginUrl('calendar');
    header('Location: '.$login);
    exit;
}

//Code is returned back from google
if(isset($_GET['code'])) {
    //save it to session
	$access = $auth->getAccess($_GET['code']);
    $_SESSION['token'] = $access['access_token'];
	
}

© 2012 Openovate Labs. All rights reserved.