PHP在XERO中交换访问令牌的验证码

a5g8bdjr  于 2023-03-07  发布在  PHP
关注(0)|答案(2)|浏览(127)

在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, 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);

在这两种情况下,我都没有得到任何回应。提前感谢任何帮助!

2w2cym1i

2w2cym1i1#

$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);

请尝试上面的代码,你会发现成功.

pvabu6sv

pvabu6sv2#

php localhost不支持curl函数。如果你使用laravel服务器,你会得到成功的响应。

相关问题