如何在 Python 中使用 PayPal REST API 和 PayPal 开发者真实帐户发送 PayPal 发票?

问题描述 投票:0回答:1

我正在开发一个桌面应用程序,它将使用 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 帐户的一些事实:

  • 我正在使用真实账户凭证(客户 ID 和客户密码)
  • 我已确认该账户有创建和发送发票的权限
  • 我正在向 Sneding Invoice API 发送 POST 请求,而不是 GET 请求

任何人都可以看到并让我知道我做错了什么吗?谢谢

python-3.x paypal paypal-rest-sdk
1个回答
0
投票

该 SDK 已弃用,请勿将其用于任何目的

v1/发票 API 也已弃用,请勿将其用于任何目的

PayPal Invoicing API 的当前版本记录在此处。仅使用它。

© www.soinside.com 2019 - 2024. All rights reserved.