WordPress Cron Jobs with Google Site Kit – Missing Required Scopes in Scheduled Event for Google Search Console Data Fetching

I'm working on a WordPress plugin where I fetch keyword data like Total Clicks, Total Impressions, Average CTR, and Average Positions from the Google Search Console using the Google Site Kit plugin. To automate this, I’ve implemented cron jobs using wp_next_scheduled() and wp_schedule_single_event() to schedule the fetching of keyword data and send an email with the results. The issue arises when I try to schedule these functions. I get the following error in my email: broken backlinks semrush: Array ( [errors] => Array ( [missing_required_scopes] => Array ( [0] => Site Kit can’t access the relevant data from Search Console because you haven’t granted all permissions requested during setup. ) ) [error_data] => Array ( [missing_required_scopes] => Array ( [status] => 403 [scopes] => Array ( [0] => https://www.googleapis.com/auth/webmasters ) ) ) ) However, when I remove the scheduling code and directly fetch the data without using cron jobs, everything works fine, and I successfully receive the following results: Keyword: broken backlinks semrush, Clicks: 0, Impressions: 2, Position: 19.5 What I've Tried: I've ensured that the Google Site Kit plugin is active and properly set up. I've checked the required permissions, and they seem to be fine when fetching data without scheduling. My Code: `<?php if ( ! class_exists( 'GSC_Data_Fetching_Cron' ) ) { class GSC_Data_Fetching_Cron { /** * Schedule an email event if not already scheduled. * * @param int $interval The interval in seconds for scheduling the email. */ public function gsc_data_schedule_email( $interval ) { if ( ! wp_next_scheduled( 'gsc_data_send_email_event_' . $interval ) ) { wp_schedule_single_event( time() + $interval, 'gsc_data_send_email_event', array( $interval ) ); } } /** * Send email with Google Search Console data. * * @param int $interval The interval for which the email event is triggered. */ public function gsc_data_send_email_function( $interval ) { global $wpdb; if ( ! class_exists( 'Google\Site_Kit\Plugin' ) ) { return; // Exit if Google Site Kit is not active. } // Fetch keyword data from Google Search Console. $site_kit = \Google\Site_Kit\Plugin::instance(); $context = $site_kit->context(); $modules = new \Google\Site_Kit\Core\Modules\Modules( $context ); $search_console = $modules->get_module( 'search-console' ); if ( ! $search_console ) { return; // Ensure the Search Console module is available. } $table_name = $wpdb->prefix . 'gsc_data_settings_table'; $settings = $wpdb->get_results( "SELECT * FROM $table_name" ); if ( ! $settings ) { return; // Exit if no threshold settings found. } $gsc_data = array(); foreach ( $settings as $setting ) { $selected_keywords = maybe_unserialize( $setting->selected_keywords ); $start_date = date( 'Y-m-d', strtotime( '-7 days' ) ); $end_date = date( 'Y-m-d' ); // Fetch Queries data from GSC. $queries_data = $search_console->get_data( 'searchanalytics', array( 'dimensions' => array( 'query' ), 'startDate' => $start_date, 'endDate' => $end_date, 'rowLimit' => 100, 'filters' => array( array( 'dimension' => 'query', 'operator' => 'in', 'expression' => $selected_keywords, ), ), ) ); // Prepare GSC data to save. $gsc_data[] = array( 'query_name' => $setting->query_name, 'gsc_data' => json_encode( $queries_data ), 'created_at' => current_time( 'mysql' ), ); } // Insert data into gsc_data table. $wpdb->insert( $wpdb->prefix . 'gsc_data', array( 'gsc_data' => json_encode( $gsc_data ), ) ); // Prepare email content. $message = '<h2>' . esc_html__( 'Google Search Console Fetched Data', 'text-domain' ) . '</h2>'; foreach ( $gsc_data as $data ) { $message .= '<p>' . esc_html( $data['query_name'] ) . ': ' . print_r( json_decode( $data['gsc_data'], true ), true ) . '</p>'; } // Send email. $to = 'waqas@gmail.com'; $subject = esc_html__( 'Google Search Console Fetched Data', 'text-domain' ); $headers = array( 'Content-Type: text/html; charset=UTF-8' ); wp_mail( $to, $subject, $message, $headers ); // Clear the scheduled hook after the email is sent. wp_clear_scheduled_hook( 'gsc_data_send_email_event_' . $interval ); } /** * Clear the cron event for GSC data email. */ public function gsc_data_clear_cron_event() { wp_clear_scheduled_hook( 'gsc_data_send_email_event' ); } } }`

Comment (0)

You’ll be in good company