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.
 
 
 

67 lines
2.5 KiB

<?php
use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Auth\HttpHandler\Guzzle7HttpHandler;
use Google\Auth\OAuth2;
use GuzzleHttp\Client;
function connectWithGooglePhotos($request, array $scopes, $redirectURI)
{
$clientSecretJson = json_decode(
file_get_contents('client_secret_1000190146810-j9mohjt8m4m8j8sj6hupl1bghp6o1pdn.apps.googleusercontent.com.json'),
true
)['web'];
// dump($clientSecretJson);exit;
$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
]);
$client = new Client(['proxy' => 'http://127.0.0.1:1087']);
$httpHandler = new Guzzle7HttpHandler($client);
// The authorization URI will, upon redirecting, return a parameter called code.
$code = $request->get('code');
if ($code == null || $code == "") {
$authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']);
// dump($authenticationUrl);
// redirect($authenticationUrl);
// header("Location: " . $authenticationUrl);
// redirect()->to($authenticationUrl);
redirect($authenticationUrl);
} else {
dump("code is " . $code);
// With the code returned by the OAuth flow, we can retrieve the refresh token.
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();
$refreshToken = $authToken['access_token'];
// The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
// they expire.
$credentials = new UserRefreshCredentials(
$scopes,
[
'client_id' => $clientId,
'client_secret' => $clientSecret,
'refresh_token' => $refreshToken
]
);
session(['credentials' => $credentials]);
// Return the user to the home page.
dump("before redirect");
redirect('google/home/index');
// header("Location: index.php");
}
}