Sage Accounting API授权请求遇DataParsingError问题排查
Sage Accounting API 授权回调数据解析错误排查
基于CBroe答案更新的代码
我使用Sage开发者文档,通过PHP类对接Sage Accounting API时遇到以下错误:
'{"$severity":"error","$dataCode":"DataParsingError","$message":"The data you sent could not be processed.","$source":"Proxy"}'
我已按认证指南创建应用并设置回调地址、密钥等,但显然存在疏漏。脚本可成功执行第一步,跳转至授权页面;但跳转回回调URL时触发上述错误。
Sage类代码
<?php class Sage { private $clientId; private $clientSecret; private $accessToken; public function __construct() { $this->clientId = '37d7c52a-2550-4685-b2e0-e03146b370f7/641d33eb-83d1-4b3f-9051-6ddcde064ec9'; $this->clientSecret = 'eie%vSrnw8sT|/(X;SIz'; } function authorizeSageAccountingApp() { $authorizationUrl = 'https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&country=gb&locale=en_GB'; $clientId = $this->clientId; $redirectUri = 'http://localhost/sailadventure-new/tests/callback'; $scopes = 'full_access'; // or 'readonly' if applicable $authorizationUrl .= '&response_type=code'; $authorizationUrl .= '&client_id=' . urlencode($clientId); $authorizationUrl .= '&redirect_uri=' . urlencode($redirectUri); $authorizationUrl .= '&scope=' . urlencode($scopes); $authorizationUrl .= '&state=159786325'; header('Location: ' . $authorizationUrl); exit(); } function handleCallback() { if (isset($_GET['code'])) { // Initialize cURL $curl = curl_init(); // Create the URL $url = 'https://oauth.accounting.sage.com/token'; $clientSecret = $this->clientSecret; $authCode = $_GET['code']; $data = array( 'grant_type' => 'authorization_code', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $authCode, 'redirect_uri' => 'http://localhost/sailadventure-new/tests/callback' ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded' ]); // Execute the request $response = curl_exec($curl); if (curl_errno($curl)) { $error = curl_error($curl); return $error; } curl_close($curl); echo '<pre>' . var_export($response, true) . '</pre>'; } } }
MVC控制器代码
<?php class Tests extends Controller { public function __construct() { } //localhost/sailadventure-new/tests public function index() { $sage = new Sage(); $sage->authorizeSageAccountingApp(); } //localhost/sailadventure-new/tests/callback public function callback() { $sage = new Sage(); $sage->handleCallback(); } //localhost/sailadventure-new/tests/continue public function continue() { $sage = new Sage(); } }
Sage开发者控制台应用详情截图

能否帮忙找出问题所在?
内容的提问来源于stack exchange,提问作者Matthew Barraud
相关产品推荐
相关产品推荐

