首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTTP 400 on payout

HTTP 400 on payout
EN

Stack Overflow用户
提问于 2017-11-26 22:39:56
回答 1查看 53关注 0票数 0

我一直在尝试为我的网站建立Paypal套现系统,我浏览了无数的Paypal API页面,并将所有的例子翻译成Python:

代码语言:javascript
复制
import requests
import string
import random
import json
from requests.auth import HTTPBasicAuth

class makePayment(object):
    def __init__(self, paypal_email, subject, amount, note, clientid, secret):
        self.access_token = self.obtain_access_token(clientid, secret) # Successfully obtained access token
        self.headers = {"Content-type": "application/json", "Authorization": "Bearer {0}".format(self.access_token)}
        self.sender_batch_id = self.generate_sender_batch_id() # Randomly generated sender_batch_id in base36 ([A-Z] and [1-9])
        self.payout_form = self.create_payout_form(self.sender_batch_id, paypal_email, subject, amount, note) # Not encoded in JSON, just normal HTML form
        self.response = requests.post("https://api.sandbox.paypal.com/v1/payments/payouts",
                                      data=self.payout_json,
                                      headers=self.headers)

    def httpResponse(self):
        return self.response

    @staticmethod
    def obtain_access_token(clientid, secret):
        headers = {"Accept": "application/json", "Accept-Language": "en_US"}
        response = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",
                                 auth=HTTPBasicAuth(clientid, secret),
                                 data={"grant_type": "client_credentials"},
                                 headers=headers)
        return dict(response.json()).get("access_token")

    @staticmethod
    def generate_sender_batch_id():
        base36 = str(string.digits + string.ascii_lowercase)
        return "".join([random.choice(base36) for char in range(1, 9)])

    @staticmethod
    def create_payout_form(sender_batch_id, paypal_email, subject, amount, note):
        payout_dict = {
            'sender_batch_header': {
                    'sender_batch_id': sender_batch_id,
                    'email_subject': subject,
            },
            'items': [{
                'recipient_type': 'EMAIL',
                'amount': {
                    'value': amount,
                    'currency': 'USD'
                },
                'receiver': paypal_email,
                'note': note,
                'sender_item_id': '${0} item'.format(amount),
            }]
        }
        return payout_dict

token generatorpayout example

我得到了http错误400 (错误的请求),在此之前,我尝试过在json中编码数据,得到http错误422 (无法处理的实体),也许json编码是正确的方法?

请注意,sender_batch_id是随机生成的,因为我不知道获得它的任何其他方法。

我做错了什么?有没有可能我遗漏了一些参数?我认为身份验证头有一些问题。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-11-26 22:44:22

大多数来自"application/ json“类型的times POST请求都需要以json格式发送:

代码语言:javascript
复制
 response = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",
                             auth=HTTPBasicAuth(clientid, secret),
                             json={"grant_type": "client_credentials"},
                             headers=headers)

我用过

代码语言:javascript
复制
json

而不是

代码语言:javascript
复制
data 

在您的POST请求中

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47497286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档