python 如何克服尝试显示发票时出现的403错误?

jaxagkaj  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(119)

我正在尝试使用PayPal REST API并将其集成到我的Python脚本中。
我想要的东西一开始听起来很简单,但在某个时候我陷入了困境:
使用我创建的应用程序(Live Dashboard),我希望通过ID从我的帐户中检索发票详细信息。PayPal为此提供了以下端点:https://api-m.paypal.com/v2/invoicing/invoices/{invoice_id}(文档)
我已经通过之前的函数获得了我的访问令牌,到目前为止,它可以正常工作。我的应用可以访问所有必要的东西:

但是,我无法接收到有关具有以下代码的某个发票ID的信息:

async def get_invoice_info(self, access_token, invoice_id: str):
        url = f"https://api-m.paypal.com/v2/invoicing/invoices/{invoice_id}"
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {access_token}'
        }

        async with aiohttp.ClientSession() as session:
            async with session.get(url, headers=headers) as resp:
                print(resp.text)
                if resp.status != 200:
                    raise Exception("Failed to retrieve invoice information!")
                return await resp.json()

打印出范围,我更困惑了:

{'scope': 'https://uri.paypal.com/services/checkout/one-click-with-merchant-issued-token https://uri.paypal.com/services/payments/realtimepayment https://uri.paypal.com/services/payments/payment/authcapture openid https://uri.paypal.com/services/payments/refund https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/billing-agreements https://api.paypal.com/v1/payments/.* https://uri.paypal.com/services/reporting/search/read https://api.paypal.com/v1/vault/credit-card/.* https://uri.paypal.com/services/shipping/trackers/readwrite https://uri.paypal.com/services/applications/webhooks', 'access_token': 'XXX', 'token_type': 'Bearer', 'app_id': 'XXX', 'expires_in': XXX, 'nonce': 'XXX'}

我可以看到端点没有列出,但无法向自己解释这种行为。我还尝试在请求访问令牌时更改scope参数,但没有任何效果:

async def get_paypal_access_token(self):

        d = {"grant_type" : "client_credentials"} # also included the scope neede here
        h = {"Accept": "application/json", "Accept-Language": "en_US"}

        async with aiohttp.ClientSession() as session:
            async with session.post('https://api.paypal.com/v1/oauth2/token', auth=aiohttp.BasicAuth(CLIENT_ID, SECRET), headers=h, data=d) as resp:
                r = await resp.json()
                print(r)
                access_token = r['access_token']
                return access_token

我通读了文档,没有找到任何说明我不能用开发人员/个人帐户执行此类操作的内容。我是否错过了什么?

我得到以下错误代码:

<bound method ClientResponse.text of <ClientResponse(https://api-m.paypal.com/v2/invoicing/invoices/) [403 Forbidden]>

mklgxw1f

mklgxw1f1#

要使用实时PayPal REST应用程序,您需要一个实时PayPal企业帐户,这是正确的。
invoicing的作用域是https://uri.paypal.com/services/invoicing。没有此作用域的访问令牌无法使用invoicing API,并且403是预期的响应。
在对任何REST应用的功能进行任何更改后,如果您之前已为该应用请求了API访问令牌,则需要等待9小时或从缓存中删除/重置该访问令牌,然后任何更改才会生效。

相关问题