【发布时间】:2013-03-16 00:05:37
【问题描述】:
使用 python 和 django,我正在尝试使用 REST API 帐户支付资源在贝宝上创建支付。当我使用 curl 时一切正常。在 Django 视图中,我得到了令牌,但是当我尝试使用它进行付款时,我收到“HTTP Error 401: Unauthorized”错误。
这是我的 curl 有效:
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()
换句话说,我正在尝试让 curl 在我看来工作。
谢谢。
【问题讨论】:
-
request1 = urllib2.Request("https://www.sandbox.paypal.com/cgi-bin/webscr") -
@catherine。我期待具有令牌的 json 数据,但使用您的地址将我带到贝宝屏幕,没有任何形式。
-
我也有同样的问题,你找到解决办法了吗?