require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setClientId('YOUR_CLIENT_ID'); $client->setClientSecret('YOUR_CLIENT_SECRET'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $access_token = $client->getAccessToken(); }
require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfigFile('PATH_TO_CLIENT_SECRET_JSON'); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->addScope('https://www.googleapis.com/auth/userinfo.email'); $authUrl = $client->createAuthUrl(); header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));In this example, the createAuthUrl method is used to generate a URL that the user can use to grant authorization. The URL is then redirected to the user's browser for authentication. After authentication, the user is redirected back to the redirect URI where the authenticate method will be called to exchange the authorization code for an access token. Package Library: Google API PHP client library.