setApplicationName($appName); $client->setClientId($clientID); $client->setClientSecret($clientSecret); $client->setRedirectUri($redirectUri); $client->addScope($scopesArray); // Check if an OAuth token is provided if ($oauthToken) { try { // Attempt to fetch the access token with the provided OAuth token $token = $client->fetchAccessTokenWithAuthCode($oauthToken); $client->setAccessToken($token); $oauth = new Oauth2($client); // Get user information using the OAuth2 service $user_info = $oauth->userinfo->get(); return $user_info; } catch (\Exception $e) { // Handle exceptions and return an error message return 'Error fetching access token: ' . $e->getMessage(); } } else { // If no OAuth token is provided, generate the authentication URL $url = $client->createAuthUrl(); // Redirect the user to the authentication URL return redirect()->to(filter_var($url, FILTER_SANITIZE_URL)); } } } ?>