ask('Enter your Google OAuth Client ID'); $client_secret = $this->ask('Enter your Google OAuth Client Secret'); $redirect_uri = $this->ask('Enter your Redirect URI (e.g., http://localhost/oauth2callback)'); $authUrl = 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query([ 'scope' => 'https://www.googleapis.com/auth/adwords', 'access_type' => 'offline', 'include_granted_scopes' => 'true', 'response_type' => 'code', 'client_id' => $client_id, 'redirect_uri' => $redirect_uri ]); $this->info("Open this URL in your browser:"); $this->line($authUrl); $this->info("After approving access, you will be redirected to your redirect URI with a 'code' parameter."); $code = $this->ask('Enter the code from the URL'); // Exchange code for refresh token $response = $this->postToken($client_id, $client_secret, $redirect_uri, $code); if(isset($response['refresh_token'])) { $this->info("Success! Your refresh token is:"); $this->line($response['refresh_token']); } else { $this->error('Failed to get refresh token.'); $this->line(json_encode($response, JSON_PRETTY_PRINT)); } return 0; } protected function postToken($client_id, $client_secret, $redirect_uri, $code) { $ch = curl_init('https://oauth2.googleapis.com/token'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'code' => $code, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => 'authorization_code' ])); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } }