PHP code example of rogerthomas84 / ohgoo
1. Go to this page and download the library: Download rogerthomas84/ohgoo 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/ */
rogerthomas84 / ohgoo example snippets
\OhGoo\OhGooConfig::getInstance()->setClientId(
'xxxx-xxxx.apps.googleusercontent.com'
)->setClientSecret(
'xxxxxxxx'
)->setRedirectUri(
'http://mydomain.tld/my-path'
);
if ($_GET['code']) {
try {
$data = \OhGoo\OhGooService::getAccessTokenFromCode($_GET['code']);
// If you have a refresh token already for a user, call the line below instead:
// $data = \OhGoo\OhGooService::getAccessTokenFromRefreshToken($theRefreshToken)
if ($data instanceof \OhGoo\Dto\OauthCredentialsDto) {
$userDto = \OhGoo\OhGooService::getGoogleUser($data);
var_dump($userDto);
exit;
}
echo 'Logging in via Google failed.';
exit;
} catch(Exception $e) {
echo sprintf(
'We experienced an error authenticating you via Google. Message: %s',
$e->getMessage()
);
exit;
}
}
header("Location: " . \OhGoo\OhGooService::getLoginUrl(
\OhGoo\OhGooService::ACCESS_TYPE_OFFLINE // OR \OhGoo\OhGooService::ACCESS_TYPE_ONLINE
));