问题描述
我在为XERO中的访问令牌交换验证码方面有些挣扎。这应该是一个简单的操作,所以我试图避免使用库的开销。这是到目前为止的代码(假设在到达此处之前,已经定义了任何变量)
// 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,CURLOPT_POST,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);
在任何情况下,我都没有得到任何回复。预先非常感谢您的帮助!
解决方法
$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);
请尝试上面的代码,您会成功。