What are the best practices for setting and managing scopes in a PHP Google OAuth implementation to avoid unexpected behavior?

When setting and managing scopes in a PHP Google OAuth implementation, it is important to carefully define the necessary scopes for your application to access specific Google APIs. Avoid requesting unnecessary scopes to minimize the risk of unauthorized access to user data. Additionally, regularly review and update the scopes used in your OAuth implementation to ensure they align with the current requirements of your application.

// Define the necessary scopes for accessing Google APIs
$scopes = array(
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/drive'
);

// Generate the OAuth URL with the specified scopes
$authUrl = $client->createAuthUrl($scopes);