1

I'm struggling a bit to exchange my verification code for an access token in XERO. It should be a straightforward operation so I'm trying to avoid the overhead of using libraries. Here's the code so far (Assume any variables have already been defined before we get to here)

// set up the concatenated string
$string = urlencode('grant_type=authorization_code&code='.$code.'&redirect_uri='.$redirecturi);

// set up the header array
$headerarray = array('authorization:Basic '.base64_encode($clientid.":".$secret), 'Content-Type: application/x-www-form-urlencoded');


// first attempt with curl

curl_setopt($ch, CURLOPT_URL, 'https://identity.xero.com/connect/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerarray);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
echo curl_exec($ch);


// second attempt with http post

$options = array(
        'http' => array(
                'header'  => $headerarray,
                'method'  => 'POST',
                'content' => $string
                )
        );
$context  = stream_context_create($options);
echo file_get_contents('https://identity.xero.com/connect/token', false, $context);

In neither case am I getting any response back. Many thanks in advance for any help!

Grissom
  • 31
  • 2

2 Answers2

0
$client_id = "045B1D1413977D";
$client_secret = "EZH_YVWzi6KRBy6sf2YnA0ge";
$code = $_REQUEST['code'];
$site_url = $sugar_config['site_url'];
$redirect_uri = "https://def376a989ec.ngrok.io/Xero.php";
$base64encode = base64_encode($client_id.":".$client_secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://identity.xero.com/connect/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=$code&redirect_uri=$redirect_uri");    
$headers = array();
$headers[] = 'authorization: Basic '.$base64encode.'';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

Please try above code and you will find success.

LOKESH
  • 1,303
  • 1
  • 16
  • 29
0

php localhost not support curl function with those credentials. If you use laravel server you will get success response.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '23 at 16:11