我正在开发一个桌面应用程序,它将使用 PayPal 的 REST API 使用 Python 创建发票并将其批量发送。我使用 paypalrestsdk 使用真实帐户(非沙箱)中的凭据(客户端 ID 和客户端密钥)创建发票,但是当我使用
send()
方法发送发票时,我的应用程序只是继续加载并在PayPal 开发者 API 调用接口,我看到收到 401 错误,这是一个权限错误。
好吧,我认为由于该库已被弃用,所以为什么不创建我自己的方法来发送发票。因此,下面是用于创建发票的
paypalrestsdk
库和使用 request
库发送发票的我自己的代码的混合:
def send_invoices_batch(self, email_list):
paypalrestsdk.configure({
"mode": "live", # sandbox or live
"client_id": str(self.clientIdInput.text()),
"client_secret": str(self.clientSecretInput.text())
})
invoices = [] # Store Invoice objects for each email
# Construct and store Invoice objects for each email
# for email in email_list:
invoice = self.construct_invoice(email_list)
invoices.append(invoice)
# Create and send invoices using the paypalrestsdk library
for invoice in invoices:
invoice_created = invoice.create()
if invoice_created:
print("INVOICE CREATED:", invoice_created)
self.responseDisplay.append(f"Invoice {invoice.id} created successfully!")
else:
self.responseDisplay.append(f"Failed to create invoice {invoice.id}: {invoice.error}")
paypalrestsdk.configure({
"mode": "live", # sandbox or live
"client_id": str(self.clientIdInput.text()),
"client_secret": str(self.clientSecretInput.text())
})
send_url = f"https://api.paypal.com/v1/invoicing/invoices/{invoice.id}/send"
headers = {
"Authorization": f"Bearer {self.get_access_token()}",
"Content-Type": "application/json"
}
response = requests.post(send_url, headers=headers)
if response.status_code == 202:
self.responseDisplay.append(f"Invoice {invoice.id} sent successfully!")
else:
self.responseDisplay.append(f"Failed to send invoice {invoice.id}: {response.text}")
def get_access_token(self):
auth_url = "https://api.paypal.com/v1/oauth2/token"
headers = {
"Accept": "application/json",
"Accept-Language": "en_US"
}
data = {
"grant_type": "client_credentials"
}
auth = (self.clientIdInput.text(), self.clientSecretInput.text())
response = requests.post(auth_url, headers=headers, data=data, auth=auth)
if response.status_code == 200:
access_token = response.json().get("access_token")
return access_token
else:
raise Exception(f"Failed to obtain access token: {response.text}")
当我现在运行我的应用程序并尝试创建和发送发票时,在发送步骤中,我在我的应用程序中收到以下错误:
无法发送发票 [INVOICE-ID]: {"name":"AUTHORIZATION_ERROR","message":"发生授权错误。","information_link":"https://developer.paypal.com/docs/api/发票/#errors","debug_id":"48832993cf8a2"}
有关我的应用程序和 PayPal 帐户的一些事实:
任何人都可以看到并让我知道我做错了什么吗?谢谢