Devlopment .net Getting unauthorized error

I have been trying to write a relatively simple payment capture application in .net and react. My problem is that I keep getting this exception:

Square.SquareApiException: ‘Error with status code 401
Status code: 401
Body: {
“errors”: [
{
“category”: “AUTHENTICATION_ERROR”,
“code”: “UNAUTHORIZED”,
“detail”: “This request could not be authorized.”
}
]
}’

I have double checked my credentials. I even used the same credential and source id in POST /v2/payments - Square API Explorer and it worked fine. The core of my code is pretty much the same as the code provided in the sample in the create payment page.

At this point, I am spinning my wheels getting no where.

Any thoughts would be helpful.

Please check that your .NET client targets the same environment matching your API Explorer test. A bare 401 when the same token works in API Explorer almost always means the SDK client is pointed at the wrong environment. The Square .NET SDK targets Production unless you set an environment, so a Sandbox access token gets rejected. Set Sandbox explicitly when you build the client:

  • Base URL (https://connect.squareupsandbox.com/v2/payments vs https://connect.squareup.com/… )
  • Application ID
  • Location ID