I am trying to get a list of all transactions from my PayPal Business account by using the Transaction Search API, but I keep getting the 400 INVALID_REQUEST response.
According to this documentation, I am doing everything correctly with the headers. I also gave access to my app to do this kind of search. Could anyone help me?
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)
Thanks
I recreated all code, generated TOKEN
, ran request and got error 400
... but in response.text
I got explanation:
"issue":"Date range is greater than 31 days"
If I change dates to
'start_date': '2021-01-01T00:00:00-0700',
'end_date': '2021-02-01T00:00:00-0700',
then it works for me.
BTW: You need (USERNAME, KEY)
only to generate TOKEN
and later you can use only TOKEN
.
My full code used for tests:
It needs only CLIENT_ID
and SECRET
because it runs code to get 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()