python - Paypal Rest API - 使用 paypal 帐户支付资源创建支付

标签 python django paypal

使用 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 来请求适当的权限。

请参阅此链接:https://developer.paypal.com/webapps/developer/docs/classic/permissions-service/integration-guide/PermissionsAbout/

关于python - Paypal Rest API - 使用 paypal 帐户支付资源创建支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444002/

相关文章:

python - MySQL 表与 python 连接

python - 从字典中存在的列表值中获取单个 "set"对象

django - FileField 无法与 Django 中的 ArrayField 一起使用

python - .env 'PAYPAL_API_USERNAME' 未定义

缺少 python gi.repository.gtk 菜单分隔符

python - matplotlib 等高线图; 'module' 对象没有属性 'nextafter'

python - 不重定向到动态 url

python - 基于 slug 的 Django 查询集过滤器

javascript - 拥有 CB 和 PayPal 的 Braintree 自定义表单

python - 安全 header 无效 (python)