404 读取余额账户Api时的响应。

问题描述 投票:0回答:1
import paypalrestsdk
import httpx


class paypal:
    def __init__(self):
        self.secret_id = 'XXXX'
        self.client_id = 'XXXX'

        self.token = ''

    def getToken(self):
        headers = {
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
        }

        data = {
            'grant_type': 'client_credentials'
        }
        response = httpx.post(url='https://api.sandbox.paypal.com/v1/oauth2/token', data=data,headers=headers,auth=(self.client_id,self.secret_id))
        response_data = response.json()
        self.token = response_data['access_token']

    def getBalance(self):
        print(self.token)
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer '+self.token,
            'Accept':'application/x-www-form-urlencoded'
        }
        response = httpx.post(url='https://api.sandbox.paypal.com/v2/wallet/balance-accounts', headers=headers)
        print(response.status_code)
        response_data = response.json()
        print(response_data)


        available = response_data['total_available'][0]['value']
        print(response_data)


if __name__ == "__main__":
    s = paypal()
    s.getToken()
    s.getBalance()

我得到404代码,我是不是做了什么坏事?

    Traceback (most recent call last):
  File "C:/Users/localhost/PycharmProjects/Telegram/paypal/Main.py", line 48, in <module>
    s.getBalance()
  File "C:/Users/localhost/PycharmProjects/Telegram/paypal/Main.py", line 37, in getBalance
    response_data = response.json()
  File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\models.py", line 899, in json
    return jsonlib.loads(self.text, **kwargs)
  File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
404

https:/developer.paypal.comdocslimited-releasebalance-accountsv2api。

我也尝试用 "Authorization: 但得到的回应是一样的,我读了文档并搜索了一下,我没有发现任何东西,而且访问令牌是新的,所以我不明白,因为我得到的访问令牌是有效的。

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

你正在发送一个 POST 请求而不是 GET 请求。使用 httpx.get 而不是 httpx.post.

你甚至可以使用 httpx_auth 以使你的代码集中在你想检索的内容上。

import paypalrestsdk
import httpx
import httpx_auth


class paypal:
    def __init__(self):
        self.auth = httpx_auth.OAuth2ClientCredentials(
            token_url='https://api.sandbox.paypal.com/v1/oauth2/token',
            client_id='XXXX',
            client_secret='XXXX',
        )

    def getBalance(self):
        response = httpx.get(url='https://api.sandbox.paypal.com/v2/wallet/balance-accounts', auth=self.auth)
        print(response.status_code)
        response_data = response.json()
        print(response_data)


        available = response_data['total_available'][0]['value']
        print(response_data)


if __name__ == "__main__":
    s = paypal()
    s.getBalance()
© www.soinside.com 2019 - 2024. All rights reserved.