(To preface this post, I have already googled and searched around the forum and found this:
However, I can’t get my head around this error in my application and I’ve spent the better part of the day trying to figure it out.)
I developed a test square integration using the old SquareConnect api about a year ago. I just returned to it this morning and realized I was getting these unauthorized errors.
First thing I did was update to the new API but am still getting the same result.
The code is very straighforward and I’ve triple checked all of my credentials. I’m at a complete loss.
The error is coming when I exchange a code for an access token.
const client = new Client({
timeout:3000,
environment: Environment.Sandbox,
accessToken: functions.config().square.token
})
console.log("accessToken: " + functions.config().square.token);
console.log("clientId: " + functions.config().square.appid);
console.log("clientSecret: " + functions.config().square.secret);
console.log("code: " + req.body.code);
try {
const response = await client.oAuthApi.obtainToken({
clientId: functions.config().square.appid,
clientSecret: functions.config().square.secret,
code: req.body.code,
grantType: 'authorization_code',
shortLived: false
});
return res.json({'success': true, data: response.httpResponse});
} catch(error) {
if (error instanceof ApiError) {
errorResult = error.errors;
} else {
errorResult = error;
}
console.log("error! " + JSON.stringify(errorResult));
return res.sendStatus(400);
}
Each time I’m getting the following error printed in the js logs:
[{"category":"V1_ERROR","code":"service.not_authorized","detail":"Not Authorized"}]
I’ve checked all of my tokens a bunch. I’ve even plugged the stuff I’ve logged in the method above directly into the API explorer after generating the code and I get a 200 so I know that my credentials are correct, and I know I’m setting my sandbox access token correctly and that it’s the correct token. (again, all this works in the api explorer).
The only thing I’m slightly confused about if I’ve done right is the sandbox test accounts. I have 2 set up and I noticed one of them is maxed out with authorizations. Since I can’t clear those easily from the dashboard I created a new sandbox and have been trying to use that when generating the code.
Any thoughts here? I’m really at a loss. My next step is just going to use an http request with the information above because this seems very simple…
Thanks,
Will