我一直在尝试为我的网站建立Paypal套现系统,我浏览了无数的Paypal API页面,并将所有的例子翻译成Python:
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_dicttoken generator,payout example
我得到了http错误400 (错误的请求),在此之前,我尝试过在json中编码数据,得到http错误422 (无法处理的实体),也许json编码是正确的方法?
请注意,sender_batch_id是随机生成的,因为我不知道获得它的任何其他方法。
我做错了什么?有没有可能我遗漏了一些参数?我认为身份验证头有一些问题。谢谢!
发布于 2017-11-26 22:44:22
大多数来自"application/ json“类型的times POST请求都需要以json格式发送:
response = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",
auth=HTTPBasicAuth(clientid, secret),
json={"grant_type": "client_credentials"},
headers=headers)我用过
json而不是
data 在您的POST请求中
https://stackoverflow.com/questions/47497286
复制相似问题