API Oauth Issues - Headers Already Sent

Hey, trying to pass my OAuth2 variables to the API library, and I keep on getting the following error:

Code:
Warning: Cannot modify header information - headers already sent by (output started at ...wp-includesscript-loader.php:1183) in [b]...vendormauticapi-librarylibAuthOAuth.php[/b] on line 659

Anyone know why this could be?

The relevant code Im using is:
Code:
require_once(plugin_dir_path(__FILE__) . 'vendor/autoload.php'); use MauticMauticApi; use MauticAuthApiAuth;

protected $mauticApi = null;

public function initialize_api() {

	if ( ! is_null( $this->mauticApi ) ) {
		return true;
	}

	/* Get the plugin settings */
	$settings = $this->get_plugin_settings();
	
	$this->log_debug( __METHOD__ . "(): Validating API info for {$settings['baseUrl']}." );

	$Mautic = ApiAuth::initiate( array(
	'baseUrl'=> $settings['baseUrl'],
	'version'      => 'OAuth2',
	'clientKey' => $settings['clientKey'], 
	'clientSecret' =>$settings['clientSecret'],
	'callback'=>$settings['callbackURI']));
	
	if($Mautic->ValidateAccessToken()) {
		if($Mautic->accessTokenUpdated()) {
			$this->$mauticApi = $Mautic;
			$this->log_debug( __METHOD__ . '(): Successful API response received.' );
			return true;
		}
	}
	
}</div>

The code its pointing to in Auth/OAuth.php is:
Code:
protected function authorize(array $scope = array(), $scope_separator = ',', $attach = null) { $authUrl = $this->_authorize_url; [...] else { //OAuth 2.0 $authUrl .= '?client_id='.$this->_client_id.'&redirect_uri='.urlencode($this->_callback); $state = md5(time().mt_rand()); $_SESSION['oauth']['state'] = $state; if ($this->_debug) { $_SESSION['oauth']['debug']['generated_state'] = $state; }
        $authUrl .= '&state='.$state.'&scope='.implode($scope_separator, $scope).$attach;
        $authUrl .= '&response_type='.$this->_redirect_type;
    }

    $this->log('redirecting to auth url '.$authUrl);

    [b]//Redirect to authorization URL
    header('Location: '.$authUrl);
    exit;[/b]
}</div>

Hey, trying to pass my OAuth2 variables to the API library, and I keep on getting the following error:

Warning: Cannot modify header information - headers already sent by (output started at ...wp-includesscript-loader.php:1183) in [b]...vendormauticapi-librarylibAuthOAuth.php[/b] on line 659

Anyone know why this could be?

The relevant code Im using is:

[code]
require_once(plugin_dir_path(FILE) . ‘vendor/autoload.php’);
use MauticMauticApi;
use MauticAuthApiAuth;

protected $mauticApi = null;

public function initialize_api() {

	if ( ! is_null( $this->mauticApi ) ) {
		return true;
	}

	/* Get the plugin settings */
	$settings = $this->get_plugin_settings();
	
	$this->log_debug( __METHOD__ . "(): Validating API info for {$settings['baseUrl']}." );

	$Mautic = ApiAuth::initiate( array(
	'baseUrl'=> $settings['baseUrl'],
	'version'      => 'OAuth2',
	'clientKey' => $settings['clientKey'], 
	'clientSecret' =>$settings['clientSecret'],
	'callback'=>$settings['callbackURI']));
	
	if($Mautic->ValidateAccessToken()) {
		if($Mautic->accessTokenUpdated()) {
			$this->$mauticApi = $Mautic;
			$this->log_debug( __METHOD__ . '(): Successful API response received.' );
			return true;
		}
	}
	
}[/code]

The code its pointing to in Auth/OAuth.php is:

[code]protected function authorize(array $scope = array(), $scope_separator = ‘,’, $attach = null)
{
$authUrl = $this->_authorize_url;
[…]
else {
//OAuth 2.0
$authUrl .= ‘?client_id=’.$this->_client_id.’&redirect_uri=’.urlencode($this->_callback);
$state = md5(time().mt_rand());
$_SESSION[‘oauth’][‘state’] = $state;
if ($this->_debug) {
$_SESSION[‘oauth’][‘debug’][‘generated_state’] = $state;
}

        $authUrl .= '&state='.$state.'&scope='.implode($scope_separator, $scope).$attach;
        $authUrl .= '&response_type='.$this->_redirect_type;
    }

    $this->log('redirecting to auth url '.$authUrl);

    [b]//Redirect to authorization URL
    header('Location: '.$authUrl);
    exit;[/b]
}[/code]

Update:
Was able to solve this by redirecting via JS:

//Redirect to authorization URL if (!headers_sent()) { header('Location: '.$authUrl); exit; } else { echo '<script type="text/javascript">'; echo 'window.location.href="'.$authUrl.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$authUrl.'" />'; echo '</noscript>'; exit; }

Is this the correct way to handle this or did I do something inherently stupid?