2013-03-16 282 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