php – I can’t start oauth xero start session in my plugin

Question

I am trying to connect users xero accounts with my app and I have that problem

Warning: session_start(): Cannot start session when headers already sent in /storage.php on line 11

my storage.php is like :

<?php
class StorageClass
{
    function __construct() {
        if( !isset($_SESSION) ){
            $this->init_session();
        }
    }

    public function init_session(){
        session_start();
    }

    public function getSession() {
        return $_SESSION['oauth2'];
    }

    public function startSession($token, $secret, $expires = null)
    {
        session_start();
    }

    public function setToken($token, $expires = null, $tenantId, $refreshToken, $idToken)
    {
        $_SESSION['oauth2'] = [
            'token' => $token,
            'expires' => $expires,
            'tenant_id' => $tenantId,
            'refresh_token' => $refreshToken,
            'id_token' => $idToken
        ];
    }

    public function getToken()
    {
        //If it doesn't exist or is expired, return null
        if (empty($this->getSession())
            || ($_SESSION['oauth2']['expires'] !== null
            && $_SESSION['oauth2']['expires'] <= time())
        ) {
            return null;
        }
        return $this->getSession();
    }

    public function getAccessToken()
    {
        return $_SESSION['oauth2']['token'];
    }

    public function getRefreshToken()
    {
        return $_SESSION['oauth2']['refresh_token'];
    }

    public function getExpires()
    {
        return $_SESSION['oauth2']['expires'];
    }

    public function getXeroTenantId()
    {
        return $_SESSION['oauth2']['tenant_id'];
    }

    public function getIdToken()
    {
        return $_SESSION['oauth2']['id_token'];
    }

    public function getHasExpired()
    {
        if (!empty($this->getSession()))
        {
            if(time() > $this->getExpires())
            {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}

and I call that class from

<?php
  ini_set('display_errors', 'On');
  require __DIR__ . '/vendor/autoload.php';
  require_once('storage.php');
   // Storage Class uses sessions for storing access token (demo only)
  // you'll need to extend to your Database for a scalable solution
  $storage = new StorageClass();
  $provider = new \League\OAuth2\Client\Provider\GenericProvider([
    'clientId'                => '*********************************',
    'clientSecret'            => '*********************************',
    'redirectUri'             => '*********************************',
    'urlAuthorize'            => 'https://login.xero.com/identity/connect/authorize',
    'urlAccessToken'          => 'https://identity.xero.com/connect/token',
    'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
  ]);

It is working fine in my localhost but when I upload the plugin in the host it give me that error.

any help here,
Thanks for you time.

0
Ahmed zidan 6 days 2022-05-13T12:49:48-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse