2013-03-16 230 views
1

使用python和django,我試圖使用REST API帳戶付款資源在PayPal上創建付款。當我使用捲曲時,一切正常。在Django視圖中,我得到了令牌,但是當我嘗試使用它進行付款時,出現「HTTP Error 401:Unauthorized」錯誤。Paypal Rest API - 使用PayPal帳戶付款資源創建付款

這裏是我的捲曲的作品:

curl -v https://api.sandbox.paypal.com/v1/payments/payment -H 'Content-Type:application/json' -H 'Authorization:Bearer ***my_token***' -d '{ "intent":"sale", "redirect_urls":{ "return_url":"http://www.myurl.com", "cancel_url":"http://www.myurl.com"}, "payer":{ "payment_method":"paypal" },"transactions":[{"amount":{ "total":"0.10", "currency":"USD"},"description":"This is the Test payment transaction description."}]}' 

這裏是有當我的問題Django的看法:

import urllib2, base64 

token = "***my_token***" 
values = { 
      "intent":"sale", 
      "redirect_urls":{ 
      "return_url":"http://www.myurl.com", 
      "cancel_url":"http://www.myurl.com" 
      }, 
      "payer":{ 
      "payment_method":"paypal" 
      }, 
      "transactions":[ 
      { 
       "amount":{ 
       "total":"0.10", 
       "currency":"USD" 
       }, 
       "description":"This is the Test payment transaction description." 
      } 
      ]} 

data = urllib.urlencode(values) 

request1 = urllib2.Request("https://api.sandbox.paypal.com/v1/payments/payment") 
base64string = base64.encodestring('%s' % token).replace('\n', '') 
request1.add_header("Content-Type", "application/json") 
request1.add_header("Authorization", "Bearer %s" % base64string) 

result1 = urllib2.urlopen(request1 , data) 
response = result1.read() 

換句話說,我努力使捲曲工作在我視圖。

謝謝。

+0

'request1 = urllib2.Request( 「https://www.sandbox.paypal.com/cgi-bin/webscr」)' – catherine 2013-03-16 01:14:55

+0

@catherine。我期待有一個令牌的JSON數據,但使用您的地址帶我到PayPal屏幕沒有任何形式。 – user1783848 2013-03-16 02:13:28

+0

我有同樣的問題,你找到的任何解決方案? – 2013-07-16 20:11:39

回答

-2

首先使用基本認證請求https://api.sandbox.paypal.com/v1/oauth2/token以獲取無記號令牌,然後使用承載令牌進行REST呼叫(https://api.sandbox.paypal.com/v1/payments/payment)。

https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/

使用貝寶SDK paypalrestsdk簡化您的Python代碼。

  1. https://github.com/paypal/rest-api-sdk-python - 自述
  2. https://github.com/paypal/rest-api-sdk-python/tree/master/samples - 樣品
+0

我可以像我指出的那樣獲取持票人令牌。我的問題是使用它來支付。 – user1783848 2013-03-16 05:38:13

+0

用'request1.add_header(「Authorization」,「Bearer%s」%bearer_token)替換'request1.add_header(「Authorization」,「Basic%s」%base64string)'' – siddick 2013-03-16 08:44:20

+0

我得到了同樣的錯誤,授權「,」承載者%s「%bearer_token) – user1783848 2013-03-16 17:07:40