Hello,
I have been tasked by a client to build a data extraction code, he provided me with an access token to apply it. The problem is that when I run my code or try the test directly (GET /v2/locations - Square API Explorer), it gives me a 401 response (UNAUTHORIZED).
I’m from the Czech Republic and he’s from the US, but I run everything through VPN on US servers.
Any idea where the problem might be, I’m including a piece of code that I tried the approaches on.
import requests
token = "TOKEN"
url="https://connect.squareup.com/v2/locations"
headers = {
"Square-Version": "2024-09-19",
"Authorization": f "Bearer {token}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
print("Response:", response.json())
else:
print(f "Error: {response.status_code}, {response.text}")
I tried Postman and your library but still the same result
Error: 401, {
"errors": [
{
"category": "AUTHENTICATION_ERROR",
"code": "UNAUTHORIZED",
"detail": "This request could not be authorized."
}
]
}
The token is correct it was tested directly by the client (US localization), and it worked for him both in Postman and in GET /v2/locations - Square API Explorer. But when I run it, the result is always 401.
from square.http.auth.o_auth_2 import BearerAuthCredentials
from square.client import Client
client = Client(
bearer_auth_credentials=BearerAuthCredentials(
access_token='TOKEN'
),
environment='production'
)
result = client.locations.list_locations()
if result.is_success():
for location in result.body['locations']:
print(f"{location['id']}: ", end="")
print(f"{location['name']}, ", end="")
print(f"{location['address']['address_line_1']}, ", end="")
print(f"{location['address']['locality']}")
elif result.is_error():
for error in result.errors:
print(error['category'])
print(error['code'])
print(error['detail'])