![](/img/trans.png)
[英]Error “unsupported_grant_type” when using the Sage One API
[英]Sage One API Error
我正在使用SageOne API PHP庫 。 它工作正常,但是如果嘗試使用get
或post
則會出現錯誤。
錯誤是
行130上的引用sage.api.php僅應傳遞變量
我的get
請求碼是
$client = new SageOne(SAGE_CLIENT_ID, SAGE_CLIENT_SECRET);
$client->setAccessToken("c7c7547xxxxxxxxxxxx8efa4f5df08f750df");
$data = array( );
$result = "";
$client = $client->get('/products', $data);
我不知道怎么了
完整代碼
require 'sage.api.php';
define('SAGE_CLIENT_ID', "fa1e8c1b114347a356d2");
define('SAGE_CLIENT_SECRET', "faaa7b353521f823ba13e3a20e72dd057c3a5fd1");
$client = new SageOne(SAGE_CLIENT_ID, SAGE_CLIENT_SECRET);
$callbackURL = 'xxxxx/addonmodules.php?module=sageone';
// We need to build the authorise url and redirect user to authorise our app
if(!$_GET['code']){
$authoriseURL = $client->getAuthoriseURL($callbackURL);
// redirect user
header("Location: ".$authoriseURL);
exit;
// We now have the authorisation code to retrieve the access token
} else {
$accessToken = $client->getAccessToken($_GET['code'], $callbackURL);
$token= $accessToken['access_token'];
$end = 'public';
$data ='';
$result = $client->get($end, $data);
echo '<pre>';
print_r($result);
sage.api.php中的代碼片段
class SageOne { ...
...
public function get($endpoint, $data=false){
return $this->call($endpoint, 'get', $data);
}
...
// error line 130 from this code
private function buildSignature($method, $url, $params, $nonce){
// uc method and append &
$signature = strtoupper($method).'&';
// percent encode bit of url before ? and append &
$signature .= rawurlencode(array_shift(explode('?', $url))).'&';
// percent encode any params and append &
if (is_array($params)){
// sort params alphabetically
$this->ksortRecursive($params);
// build query string from params, encode it and append &
$signature .= str_replace(
array('%2B'),
array('%2520'),
rawurlencode(http_build_query($params, '', '&'))
).'&';
// params can be string
} else {
// build query string from params, encode it and append &
$signature .= rawurlencode($params).'&';
}
// add 'nonce' - just use an md5
$signature .= $nonce;
// now generate signing key
$signingKey = rawurlencode($this->signingSecret).'&'.rawurlencode($this->accessToken);
// encode using sha 1, then base64 encode
$finalSignature = base64_encode(hash_hmac('sha1', $signature, $signingKey, true));
return $finalSignature;
}
這是我能看到所有重要代碼的最短時間
這是由於試圖將一個函數或方法的結果直接返回給另一個函數或方法...結果沒有引用。
因此,例如:
$obj->method(doSomething(), 'asdf', 'qwerty');
該錯誤意味着您應該在傳遞doSomething()
值之前將其賦值。
$result = doSomething();
$obj->method($result, 'asdf', 'qwerty');
另請參見: 僅變量應通過引用傳遞
可以定義一個函數(在本例中$client->get()
)來通過reference接收其參數。 這意味着它可以直接修改這些參數。 因此,如果您調用$client->get($a, $b)
,該函數可能會更改$a
和$b
的值。
顯然,它只能更改變量的值,因此,當一個函數通過引用接收參數時,您必須向其傳遞一個變量,而不是字符串,整數或對另一個函數的直接調用。
因此,如果函數$client->get()
通過引用接收其第一個參數,則以下任何一項均無效:
$client->get('string', $data);
$client->get(15, $data); // int
$client->get(other_function_call(), $data);
$client->get(12.5, $data); // float
$client->get(array(), $data);
您必須這樣做:
$a = 'string';
$client->get($a, $data);
或$a =
等等 ,可以是字符串,整數,函數調用。 關鍵是(必須在錯誤消息中明確指出)您必須傳遞一個變量 。 因此,將要傳遞的內容保存為變量,然后傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.