2013-03-26 35 views
2

按照貝寶開發者的指令「讓你的第一個電話」:使用Ruby路邊寶石訪問貝寶API

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" 

這是工作和取得預期的結果,如指令的狀態,但我更喜歡使用Ruby路邊的寶石:

require 'curl' 

paypal_result = Curl::Easy.http_post("https://api.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials") do |http| 
    http.headers['Accept'] = "application/json" 
    http.headers['Accept-Language'] = "en_US" 
    http.username = "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" 
end 

puts paypal_result.body_str 

但是我有以下幾點:

{"error":"invalid_client","error_description":"The basic auth authorization header cannot be decoded"} 

這是肯定的錯誤,但WH在我的遏制語法錯了?任何想法?

+0

謝謝喬納森。我使用詳細,我用userpwd替換了用戶名,但它不工作:( – 2013-03-26 10:24:24

+0

您可以使用[paypal-sdk-rest](https://github.com/paypal/rest-api-sdk-ruby)gem簡化你的代碼 – siddick 2013-03-27 16:17:20

回答

1

我不能告訴你到底發生了什麼問題,但我可以告訴你如何看看發生了什麼。嘗試把它變成冗長模式,所以你可以看到實際上正在發送的數據和標題:

curl = Curl::Easy.new 

# Make it verbose, prints to stderr 
curl.verbose = true 

paypal_result = curl.http_post("https://api.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials") do |http| 
    # etc. 
end 

您可以通過使用-v標誌和CLI版本進行比較。

希望能幫助你發現差異。

+1

最後我發現了問題......我必須設置http_auth_types ='basic',你對verbose的建議真的幫了我很多 – 2013-03-26 15:49:34