2013-10-05 31 views
0

我試圖找出關於貝寶自適應付款的API documentation。所以我想翻譯這個curl命令(例子):捲毛錯誤:不支持grant_type

curl https://api.sandbox.paypal.com/v1/oauth2/token \ 
-H "Accept: application/json" \ 
-H "Accept-Language: en_US" \ 
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \ 
-d "grant_type=client_credentials" 

到PHP(未顯示的僅僅是我的clientID的和祕密的):

$data = 
    'client_id=' . $clientID . '&' . 
    'client_secret=' . $clientSecret . '&' . 
    "grant_type=client_credentials"; 

$url = "https://api.sandbox.paypal.com/v1/oauth2/token"; 
$headers = array(
    'Accept' => 'application/json', 
    'Accept-Language' => 'en_US', 
    "grant_type=client_credentials" 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret); 
$x = json_decode(curl_exec($ch)); 

var_dump($x); 

此打印:

object(stdClass)#1 (2) { ["error"]=> string(22) "unsupported_grant_type" ["error_description"]=> string(22) "Unsupported grant_type" } 

我在搞什麼?任何指針,指令或提示?我已經研究了三天的文檔,但它非常乾燥,沒有好的教程似乎存在。謝謝。

回答

3

我不知道爲什麼,而且我仍然接受任何可以解釋原因的答案,但用代替json_encode($data)解決了我的問題。我想我仍然對JSON感到困惑,因爲它似乎沒有這個工作。

它現在給出它應該給出的答案。我會在幾天後回來,當我再次令人難以置信的困惑;)貝寶API糟透了。

+1

那麼,這是因爲/ v1/oauth2/token調用將編碼數據作爲輸入。然而,/ v1/payments&/ v1/vault調用會將JSON編碼數據作爲輸入,您需要在那裏調用json_encode。所有調用的輸出數據格式都是json,這在響應中的內容類型標題中應該很明顯。 – aydiv