Cannot add user to discourse group

Hi i am using wp discourse plugin in order to sync user between wordpress website and discourse community form so here i am using a code in order to add user into the discourse group when user register on the website

function mepr_capture_new_member_signup_completed($event) {
    $user = $event->get_data();
    $user_id = $user->ID;
    
    // Get user data based on user ID
    $user_data = get_userdata($user_id);
	
    // Log debug information about the user data
    log_debug_info("debug_log.txt", "User Data: ", print_r($user_data, true));

    // Check if user data exists
    if ($user_data) {
        // Get the user roles
        $roles = $user_data->roles;

        // Check if the user does not have the "browns" role or does have it
        if (!in_array('browns', $roles) || in_array('browns', $roles)) {
            // Remove all existing roles
            foreach ($roles as $role) {
                $user_data->remove_role($role);
            }

			// Add the "browns" role
			$user_data->add_role('browns');
			log_debug_info("debug_log.txt", "Assigned Browns Role: ", print_r($user_data, true));

			if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
				// Add user to the Discourse group for "browns"
				$s2member_level_1_groups = get_option('s2member_level_1_groups', '');
				log_debug_info("debug_log.txt", "s2member level 1 groups: ", print_r($s2member_level_1_groups, true));

				// Ensure $s2member_level_1_groups is not empty
				if (!empty($s2member_level_1_groups)) {
					$groups = explode(',', $s2member_level_1_groups);
					foreach ($groups as $group) {
						$group = trim($group); // Trim whitespace from group names
						log_debug_info("debug_log.txt", "Attempting to add user to Discourse group: ", $group);
						
						$result = DiscourseUtilities::add_user_to_discourse_group($user_id, $group);
						
						if (is_wp_error($result)) {
							$error_message = $result->get_error_message();
							log_debug_info("debug_log.txt", "Failed to add to Discourse Group: ", $error_message);
							log_debug_info("debug_log.txt", "Error Data: ", print_r($result->get_error_data(), true));
						} else {
							log_debug_info("debug_log.txt", "Successfully added to Discourse Group: ", $group);
						}
					}
				} else {
					log_debug_info("debug_log.txt", "No Discourse groups found in settings.", "");
				}
			}

            // Additional SSO parameters if needed
            $sso_params = array();
            $sso_params = apply_filters('wpdc_sso_params', $sso_params, $user_data);

        } else {
            // Collect all level groups
            $s2member_level_1_groups = get_option('s2member_level_1_groups', '');
            $s2member_level_2_groups = get_option('s2member_level_2_groups', '');
            $s2member_level_3_groups = get_option('s2member_level_3_groups', '');
            $s2member_level_4_groups = get_option('s2member_level_4_groups', '');

            // Combine all groups into one array
            $s2member_level_groups = array_merge(
                explode(',', $s2member_level_1_groups),
                explode(',', $s2member_level_2_groups),
                explode(',', $s2member_level_3_groups),
                explode(',', $s2member_level_4_groups)
            );

            // Remove duplicates
            $s2member_level_groups = array_unique($s2member_level_groups);

            // Remove user from all collected groups
            foreach ($s2member_level_groups as $group) {
                $group = trim($group); // Trim whitespace from group names
                $result = DiscourseUtilities::remove_user_from_discourse_group($user_id, $group);

                if (is_wp_error($result)) {
                    $error_message = $result->get_error_message();
                    log_debug_info("debug_log.txt", "Failed to remove from Discourse Group: ", $error_message);
                } else {
                    log_debug_info("debug_log.txt", "Successfully removed from Discourse Group: ", $group);
                }
            }
        }
    } else {
        // Handle cases where $user_id or $role is not provided
        // This could be logged or handled differently based on your requirements
        // For now, let's log a message
        error_log("User ID or role not provided in user_registered_callback.");
    }
}

add_action('mepr-event-member-signup-completed', 'mepr_capture_new_member_signup_completed');

and here is the response i am getting

=== User Data:  ===
WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 463
            [user_login] => sibahmed
            [user_pass] => $P$BqaZsm5xZuNLEt2oWmypNGQ1QRyoUU0
            [user_nicename] => sibahmed
            [user_email] => sibahmed82@gmail.com
            [user_url] => 
            [user_registered] => 2024-08-04 09:42:50
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => sib ahmed
        )

    [ID] => 463
    [caps] => Array
        (
            [subscriber] => 1
            [browns] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [0] => subscriber
            [1] => browns
        )

    [allcaps] => Array
        (
            [read] => 1
            [level_0] => 1
            [upload_files] => 1
            [access_s2member_level0] => 1
            [subscriber] => 1
            [browns] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)


=== Assigned Browns Role:  ===
WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 463
            [user_login] => sibahmed
            [user_pass] => $P$BqaZsm5xZuNLEt2oWmypNGQ1QRyoUU0
            [user_nicename] => sibahmed
            [user_email] => sibahmed82@gmail.com
            [user_url] => 
            [user_registered] => 2024-08-04 09:42:50
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => sib ahmed
            [user_level] => 0
        )

    [ID] => 463
    [caps] => Array
        (
            [browns] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [0] => browns
        )

    [allcaps] => Array
        (
            [read] => 1
            [browns] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)


=== s2member level 1 groups:  ===
Member,staff

=== Attempting to add user to Discourse group:  ===
Member

=== Failed to add to Discourse Group:  ===
An invalid response was returned from Discourse

=== Error Data:  ===
Array
(
    [http_code] => 404
    [http_body] => {"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}
)


=== Attempting to add user to Discourse group:  ===
staff

=== Failed to add to Discourse Group:  ===
An invalid response was returned from Discourse

=== Error Data:  ===
Array
(
    [http_code] => 404
    [http_body] => {"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}
)

even i am using this class on the top of functions.php file

use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

please help in this case

Thank You

Hey, can you check if the API Key is correct and that the Publishing Username is an admin user? :thinking:

can you please tell me about publishing username in wordpress in the discourse settings or where this setting located? and also i am not publishing posts from wordpress to discourse

Sorry for the late answer.

Your WP Discourse utility function relies on the API key and username.
The options are located in the general settings:

You must provide a valid API key and username here; the username must be an admin.

Can you check if that’s the case?