【问题标题】:PayPal API - Transaction Search API: Response 400 INVALID REQUESTPayPal API - 交易搜索 API:响应 400 INVALID REQUEST
【发布时间】:2021-06-19 01:11:36
【问题描述】:

我正在尝试使用 Transaction Search API 从我的 PayPal Business 帐户获取所有交易的列表,但我不断收到 400 INVALID_REQUEST 响应。

根据this documentation,我用标题正确地做所有事情。 我还允许访问我的应用程序来进行这种搜索。谁能帮帮我?

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)

谢谢

【问题讨论】:

  • 您是如何创建此代码的? POSTMAN 具有为不同语言生成代码的功能。你用postman生成代码了吗?
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 您是否使用由token 生成的postman 链接到Live/Production 环境?
  • 你检查print(response.text)了吗?我重新创建了所有代码,运行它并得到错误400 但在response.text 我得到了解释:"issue":"Date range is greater than 31 days" 如果我更改日期,那么它对我有用

标签: python paypal python-requests http-status-code-400


【解决方案1】:

我重新创建了所有代码,生成了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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2017-04-06
    • 1970-01-01
    • 2021-02-08
    • 2017-10-30
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多