The API version our system is using is 2020-12-16, which could be my problem, but I would almost expect to see an old version error instead of the auth error
we are getting. I wanted to ask if there is something I can look into before I went through the 26 release notes looking to see if I can safely update to the latest API. We are hoping to go live this week.
I don’t have any trouble making payments using the sandbox. The server is using the production app ID, access token and location ID (although it doesn’t look like that is used for payment.) The Android app is also using the production app ID.
The only error I get back is
code=[UNAUTHORIZED], detail=[This request could not be authorized.]
Your question was precisely the right question, thank you. That made me think about the SquareClient builder, and I then saw I was using Environment.SANDBOX instead of PRODUCTION.
The error I was seeing was happening when I called the com.squareup.square.api.CustomersApi#searchCustomers in my findCustomer() method.
Having fixed that, the code now works.
protected CustomersApi squareCustomersApi() {
SquareClient squareClient = new SquareClient.Builder()
.environment(Environment.SANDBOX) //TODO lee this needs to be
.accessToken(mustLoadEnvironmentVariable(SQUARE_ACCESS_TOKEN_ENV_VAR))
.build();
return squareClient.getCustomersApi();
}
public Customer findCustomer(User user) throws IOException, ApiException, ResponseException {
CustomersApi customersApi = squareCustomersApi();
CustomerTextFilter bodyQueryFilterEmailAddress = new CustomerTextFilter.Builder()
.exact(user.getEmail())
.build();
CustomerFilter bodyQueryFilter = new CustomerFilter.Builder()
.emailAddress(bodyQueryFilterEmailAddress)
.build();
CustomerSort bodyQuerySort = new CustomerSort.Builder()
.field("CREATED_AT")
.order("ASC")
.build();
CustomerQuery bodyQuery = new CustomerQuery.Builder()
.filter(bodyQueryFilter)
.sort(bodyQuerySort)
.build();
SearchCustomersRequest body = new SearchCustomersRequest.Builder()
.limit(10L)
.query(bodyQuery)
.build();
SearchCustomersResponse searchResponse = customersApi.searchCustomers(body);