You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
2.4 KiB

<?php
namespace App\Utils;
use Exception;
use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Auth\OAuth2;
use Illuminate\Support\Facades\Log;
class CommonUtils {
public static function randomSleep(int $level = 1000) {
try {
usleep(random_int(1000, 10000) * $level);
} catch (Exception $e) {
Log::error("sleep error message: {$e}");
}
}
public static function connectWithGooglePhotos(array $scopes, $redirectURI)
{
$clientSecretJson = json_decode(
file_get_contents('../../client_secret.json'),
true
)['web'];
$clientId = $clientSecretJson['client_id'];
$clientSecret = $clientSecretJson['client_secret'];
$oauth2 = new OAuth2([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
// Where to return the user to if they accept your request to access their account.
// You must authorize this URI in the Google API Console.
'redirectUri' => $redirectURI,
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'scope' => $scopes,
'expiry' => \Google\Auth\OAuth2::DEFAULT_EXPIRY_SECONDS *24
]);
// The authorization URI will, upon redirecting, return a parameter called code.
if (!isset($_GET['code'])) {
$authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']);
header("Location: " . $authenticationUrl);
} else {
// With the code returned by the OAuth flow, we can retrieve the refresh token.
$oauth2->setCode($_GET['code']);
$authToken = $oauth2->fetchAuthToken();
$refreshToken = $authToken['access_token'];
// The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
// they expire.
$_SESSION['credentials'] = new UserRefreshCredentials(
$scopes,
[
'client_id' => $clientId,
'client_secret' => $clientSecret,
'refresh_token' => $refreshToken
]
);
// Return the user to the home page.
redirect('/google/home/index');
// header("Location: index.php");
}
}
}