I’m having the same problem, yet my configuration seems to be correct. Using the square nodejs client v8.0.1, I’m calling:
const tokenRequest: ObtainTokenRequest = {
clientId: process.env.SQUARE_APP_ID,
clientSecret: process.env.SQUARE_ACCESS_TOKEN,
code: authCode, // comes from auth page
grantType: 'authorization_code',
};
const oauthResponse = await squareClient.oAuthApi.obtainToken(tokenRequest);
And the square client throws the following error:
request: {
method: 'POST',
url: 'https://connect.squareupsandbox.com/oauth2/token',
headers: {
'user-agent': 'Square-TypeScript-SDK/8.0.0',
'content-type': 'application/json',
'Square-Version': '2020-12-16',
accept: 'application/json'
},
body: {
type: 'text',
content: '{"client_id":"sandbox-sq0idb-HeS-xF3scAF_i15WL4VRaA","client_secret":"<SQUARE_ACCESS_TOKEN>","code":"sandbox-sq0cgb-JZaxfWumFVIBLgdpQ80TEg","grant_type":"authorization_code"}'
}
},
statusCode: 401,
headers: {
'access-control-allow-headers': 'Content-Type, Authorization, Accept',
'access-control-expose-headers': 'Link',
'content-type': 'application/json',
vary: 'Origin, Accept-Encoding',
'x-content-type-options': 'nosniff',
'x-download-options': 'noopen',
'x-frame-options': 'DENY',
'x-permitted-cross-domain-policies': 'none',
'x-request-id': 'CgjJXIaq_j7bChABGBAiCQiniIOsoqfuAg',
'x-xss-protection': '1; mode=block',
date: 'Tue, 19 Jan 2021 05:31:11 GMT',
'content-length': '70',
'strict-transport-security': 'max-age=631152000; includeSubDomains; preload',
connection: 'close'
},
body: '{\n "message": "Not Authorized",\n "type": "service.not_authorized"\n}\n',
result: { message: 'Not Authorized', type: 'service.not_authorized' },
errors: [
{
category: 'V1_ERROR',
code: 'service.not_authorized',
detail: 'Not Authorized',
field: undefined
}
]
}
(SQUARE_ACCESS_TOKEN matches the environment variable used above)
And the square client was initialized like so:
export const squareClient = new Client({
environment: process.env.NODE_ENV === 'production' ? Environment.Production : Environment.Sandbox,
accessToken: process.env.SQUARE_ACCESS_TOKEN,
});