Fix Google Sheets API integration - resolve valueInputOption error
- Added required 'valueInputOption' => 'USER_ENTERED' parameter to all Google Sheets API calls - Fixed OAuth callback handler to trigger on template_redirect hook - Enhanced debugging for OAuth token exchange process - All 7 data population methods now include proper API parameters - Resolves 'valueInputOption is required but not specified' error 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
188392cd3d
commit
7c0cbeac45
2 changed files with 64 additions and 6 deletions
|
|
@ -45,8 +45,8 @@ class HVAC_Google_Sheets_Auth {
|
|||
// Load stored access token from WordPress options
|
||||
$this->load_access_token();
|
||||
|
||||
// Register callback handler
|
||||
add_action('init', array($this, 'handle_oauth_callback'));
|
||||
// Register callback handler - use template_redirect to catch it before page rendering
|
||||
add_action('template_redirect', array($this, 'handle_oauth_callback'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -78,11 +78,21 @@ class HVAC_Google_Sheets_Auth {
|
|||
'code' => $auth_code
|
||||
);
|
||||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Token exchange request params: " . json_encode(array(
|
||||
'client_id' => substr($this->client_id, 0, 20) . '...',
|
||||
'redirect_uri' => $this->redirect_uri,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => substr($auth_code, 0, 20) . '...'
|
||||
)), 'GoogleSheets');
|
||||
}
|
||||
|
||||
$response = wp_remote_post($this->token_url, array(
|
||||
'body' => $params,
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
)
|
||||
),
|
||||
'timeout' => 30
|
||||
));
|
||||
|
||||
if (is_wp_error($response)) {
|
||||
|
|
@ -90,7 +100,14 @@ class HVAC_Google_Sheets_Auth {
|
|||
return false;
|
||||
}
|
||||
|
||||
$response_code = wp_remote_retrieve_response_code($response);
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Token exchange response code: " . $response_code, 'GoogleSheets');
|
||||
HVAC_Logger::info("Token exchange response body: " . $body, 'GoogleSheets');
|
||||
}
|
||||
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if (isset($data['access_token'])) {
|
||||
|
|
@ -98,7 +115,13 @@ class HVAC_Google_Sheets_Auth {
|
|||
if (isset($data['refresh_token'])) {
|
||||
$this->refresh_token = $data['refresh_token'];
|
||||
}
|
||||
$this->token_expiry = time() + $data['expires_in'];
|
||||
$this->token_expiry = time() + (int)$data['expires_in'];
|
||||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Successfully received tokens. Access token: " . substr($this->access_token, 0, 20) . "...", 'GoogleSheets');
|
||||
HVAC_Logger::info("Refresh token: " . ($this->refresh_token ? 'RECEIVED' : 'NOT RECEIVED'), 'GoogleSheets');
|
||||
HVAC_Logger::info("Token expires at: " . date('Y-m-d H:i:s', $this->token_expiry), 'GoogleSheets');
|
||||
}
|
||||
|
||||
// Save tokens
|
||||
$this->save_tokens();
|
||||
|
|
@ -106,7 +129,7 @@ class HVAC_Google_Sheets_Auth {
|
|||
return true;
|
||||
}
|
||||
|
||||
$this->log_error('Invalid token response: ' . $body);
|
||||
$this->log_error('Invalid token response (status ' . $response_code . '): ' . $body);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -282,7 +305,17 @@ class HVAC_Google_Sheets_Auth {
|
|||
'created_at' => time()
|
||||
);
|
||||
|
||||
update_option('hvac_google_sheets_tokens', $token_data);
|
||||
$result = update_option('hvac_google_sheets_tokens', $token_data);
|
||||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Saving tokens to database: " . ($result ? 'SUCCESS' : 'FAILED'), 'GoogleSheets');
|
||||
HVAC_Logger::info("Token data: " . json_encode(array(
|
||||
'access_token' => substr($this->access_token, 0, 20) . '...',
|
||||
'refresh_token' => $this->refresh_token ? 'SET' : 'NOT SET',
|
||||
'expires_at' => date('Y-m-d H:i:s', $this->token_expiry),
|
||||
'created_at' => date('Y-m-d H:i:s', time())
|
||||
)), 'GoogleSheets');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -345,6 +378,16 @@ class HVAC_Google_Sheets_Auth {
|
|||
* Handle OAuth callback from Google
|
||||
*/
|
||||
public function handle_oauth_callback() {
|
||||
// Debug: Log all OAuth callback attempts
|
||||
if (isset($_GET['code']) && isset($_GET['scope'])) {
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("OAuth callback detected - URI: " . $_SERVER['REQUEST_URI'], 'GoogleSheets');
|
||||
HVAC_Logger::info("OAuth callback - code param: " . substr($_GET['code'], 0, 20) . "...", 'GoogleSheets');
|
||||
}
|
||||
error_log("HVAC Google OAuth callback detected - URI: " . $_SERVER['REQUEST_URI']);
|
||||
error_log("HVAC Google OAuth callback - code: " . substr($_GET['code'], 0, 20) . "...");
|
||||
}
|
||||
|
||||
// Check if this is an OAuth callback request to the Google Sheets page
|
||||
if (isset($_GET['code']) && isset($_GET['scope']) &&
|
||||
(strpos($_SERVER['REQUEST_URI'], '/google-sheets/') !== false ||
|
||||
|
|
@ -354,11 +397,19 @@ class HVAC_Google_Sheets_Auth {
|
|||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Processing OAuth callback with code: " . substr($auth_code, 0, 20) . "...", 'GoogleSheets');
|
||||
HVAC_Logger::info("Current redirect URI: " . $this->redirect_uri, 'GoogleSheets');
|
||||
}
|
||||
|
||||
// Exchange the authorization code for tokens
|
||||
$success = $this->exchange_code_for_tokens($auth_code);
|
||||
|
||||
if (class_exists('HVAC_Logger')) {
|
||||
HVAC_Logger::info("Token exchange result: " . ($success ? 'SUCCESS' : 'FAILED'), 'GoogleSheets');
|
||||
if (!$success) {
|
||||
HVAC_Logger::error("Token exchange error: " . $this->get_last_error(), 'GoogleSheets');
|
||||
}
|
||||
}
|
||||
|
||||
if ($success) {
|
||||
// Redirect to Google Sheets admin page with success message (clean URL)
|
||||
wp_redirect(add_query_arg(array(
|
||||
|
|
|
|||
|
|
@ -193,6 +193,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'System Overview!A1:B10',
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => array(
|
||||
array('HVAC Community Events - System Overview', ''),
|
||||
array('Generated', date('Y-m-d H:i:s')),
|
||||
|
|
@ -242,6 +243,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'Trainer Performance!A1:E' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
@ -280,6 +282,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'All Events!A1:F' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
@ -315,6 +318,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'Revenue Analytics!A1:C' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
@ -348,6 +352,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'Event Details!A1:B' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
@ -389,6 +394,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'Attendees!A1:C' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
@ -421,6 +427,7 @@ class HVAC_Google_Sheets_Manager {
|
|||
$data = array(
|
||||
'range' => 'Financial Summary!A1:B' . (count($values)),
|
||||
'majorDimension' => 'ROWS',
|
||||
'valueInputOption' => 'USER_ENTERED',
|
||||
'values' => $values
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue