1. Go to this page and download the library: Download martenb/google 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/ */
martenb / google example snippets
namespace App\Presenters;
use Google_Service_Oauth2;
use InvalidArgumentException;
use MartenB\Google\GoogleLogin;
use Nette\Application\Responses\RedirectResponse;
use Nette\Application\UI\Presenter;
use Nette\Security\AuthenticationException;
final class SignPresenter extends Presenter
{
/** @var GoogleLogin @inject */
public $googleLogin;
public function actionGoogle()
{
// Redirect to Google and ask customer to grant access to his account
$url = $this->googleLogin->getLoginUrl($this->link('//googleAuthorize'), [Google_Service_Oauth2::USERINFO_PROFILE, Google_Service_Oauth2::USERINFO_EMAIL]);
$this->sendResponse(new RedirectResponse($url));
}
/**
* Log in user with accessToken obtained after redirected from Google
*
* @param $code
* @return void
*/
public function actionGoogleAuthorize($code)
{
// Fetch User data from Google and try to login
try {
$token = $this->googleLogin->getAccessToken($this->link('//googleAuthorize'), $code);
$this->user->login('google', $this->googleLogin->getMe($token));
$this->flashMessage('Login successful :-).', 'success');
} catch (InvalidArgumentException | AuthenticationException $e) {
$this->flashMessage('Login failed. :-( Try again.', 'danger');
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.