PayPal API - 交易搜索 API:响应 400 无效请求

PayPal API - Transaction Search API: Response 400 INVALID REQUEST

我正在尝试使用交易搜索 API 从我的 PayPal 企业帐户中获取所有交易的列表,但我一直收到 400 INVALID_REQUEST 响应。

根据 this documentation,我用 headers 做的一切都是正确的。 我还授予访问我的应用程序的权限以进行此类搜索。谁能帮帮我?

import requests, json

USERNAME = <MY USERNAME>
KEY = <MY SECRET KEY>
TOKEN = <MY TOKEN - GENERATED BY POSTMAN>

headers = {"Content-Type": "application/json",
           "Accept-Language": "en_US",
           "Authorization": "Bearer <MY TOKEN>",
           "Accept": "application/json"
          }

LINK = "https://api-m.paypal.com"
GET = "/v1/reporting/transactions?start_date=2021-01-01T00:00:00-0700&end_date=2021-06-01T00:00:00-0700"
GET_LINK = LINK + GET

response = requests.get(GET_LINK, auth=(USERNAME, KEY), headers=headers)
print(response)

谢谢

我重新创建了所有代码,生成了 TOKEN、运行 请求并收到错误 400

...但在 response.text 我得到了解释:

"issue":"Date range is greater than 31 days"

如果我将日期更改为

        'start_date': '2021-01-01T00:00:00-0700',
        'end_date':   '2021-02-01T00:00:00-0700',

那么对我有用。

顺便说一句: 你只需要 (USERNAME, KEY) 来生成 TOKEN 之后你只能使用 TOKEN.


我用于测试的完整代码:

它只需要 CLIENT_IDSECRET 因为它运行代码得到 TOKEN

import requests

# --- constants ---

CLIENT_ID = "ARg..."  # 80 chars
SECRET    = "EAl..."  # 80 chars

#ENDPOINT = "https://api-m.sandbox.paypal.com"  # Sandbox - doesn't have access to transactions
ENDPOINT = "https://api-m.paypal.com"          # Live

DEBUG = True

# --- functions ---

def display_response(response):
    print('response:', response)
    print('url:', response.url)
    print('text:', response.text)    

def display_data(data):
    for key, value in data.items():
        if key == 'scope':
            for item in value.split(' '):
                print(key, '=', item)
        else:
            print(key, '=', value)
    
def get_token():
    if DEBUG:
        print('--- get token ---')
    
    url = ENDPOINT + '/v1/oauth2/token'
    
    headers = {
        "Accept": "application/json",
        "Accept-Language": "en_US",
    }
        
    payload = {
        "grant_type": "client_credentials"
    }
        
    response = requests.post(url, auth=(CLIENT_ID, SECRET), data=payload)

    if DEBUG:
        display_response(response)    
    
    data = response.json()
    
    if DEBUG:
        display_data(data)
    
    return data['access_token']

def get_transactions():
    if DEBUG:
        print('--- transaction ---')
    
    url = ENDPOINT + "/v1/reporting/transactions"
    
    headers = {
        "Content-Type": "application/json",
        "Accept-Language": "en_US",
        "Authorization": f"Bearer {TOKEN}",
        "Accept": "application/json"
    }
    
    payload = {
        'start_date': '2021-01-01T00:00:00-0700',
        'end_date':   '2021-02-01T00:00:00-0700',
    }    
    
    response = requests.get(url, headers=headers, params=payload)

    if DEBUG:
        display_response(response)    
    
    data = response.json()

    if DEBUG:
        display_data(data)
        
# --- main ---

TOKEN = get_token()

print('--- token ---')
print('TOKEN:', TOKEN)

get_transactions()