# API Oauth Issues - Headers Already Sent

**URL:** https://forum.mautic.org/t/api-oauth-issues-headers-already-sent/3618
**Category:** Product Support
**Created:** [April 27, 2016, 11:22pm UTC](https://forum.mautic.org/t/api-oauth-issues-headers-already-sent/3618 "2016-04-27T23:22:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![justlevine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/justlevine/32/74_2.png) [@justlevine](https://forum.mautic.org/u/justlevine)
#### Post date: [April 27, 2016, 11:22pm UTC](https://forum.mautic.org/t/api-oauth-issues-headers-already-sent/3618/1 "2016-04-27T23:22:36Z")

</div>

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>

```

---

<div class="post-metadata">

### Author: ![justlevine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/justlevine/32/74_2.png) [@justlevine](https://forum.mautic.org/u/justlevine)
#### Post date: [April 27, 2016, 11:22pm UTC](https://forum.mautic.org/t/api-oauth-issues-headers-already-sent/3618/2 "2016-04-27T23:22:36Z")

</div>

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

```auto
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]
```

---

<div class="post-metadata">

### Author: ![justlevine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/justlevine/32/74_2.png) [@justlevine](https://forum.mautic.org/u/justlevine)
#### Post date: [April 28, 2016, 12:34pm UTC](https://forum.mautic.org/t/api-oauth-issues-headers-already-sent/3618/3 "2016-04-28T12:34:40Z")

</div>

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?
