使用 python 和 django,我正在尝试使用 REST API 帐户支付资源在 paypal 上创建支付。当我使用 curl 时一切正常。在 Django View 中,我可以正常获取 token ,但是当我尝试使用它进行付款时,出现“HTTP 错误 401:未经授权”错误。
这是我的 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 View ,在以下情况下出现问题:
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 在我看来起作用。
谢谢。
最佳答案
不确定您是否遇到了与我相同的问题,但我正在尝试为我没有正确请求权限的第三方 paypal 帐户创建付款。原来我需要使用 Permissions API 来请求适当的权限。
关于python - Paypal Rest API - 使用 paypal 帐户支付资源创建支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444002/