In my Typescript code, I have implemented a function that retrieves the access_token
and id_token
by exchanging an Authorization code received after user authorization through Google OAuth.
public async getGoogleOAuthTokens({ code }: { code: string }): Promise<GoogleTokensResult> {
const rootURL = 'https://oauth2.googleapis.com/token';
const values = {
code,
client_id: GOOGLE_AUTH_CLIENT_ID,
client_secret: GOOGLE_AUTH_CLIENT_SECRET,
redirect_uri: GOOGLE_AUTH_REDIRECT_URL,
grant_type: 'authorization_code',
};
const authTokenURL = `${rootURL}?${qs.stringify(values)}`;
try {
const res = await axios.post<GoogleTokensResult>(authTokenURL, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
return res.data;
} catch (error: any) {
console.log(error.message);
throw new Error(error);
}
}
Although this code was functioning correctly before, it has recently started displaying the error below:
>> StatusCode:: 500, Message:: AxiosError: Request failed with status code 401
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:399:5)
at ServerResponse.setHeader (node:_http_outgoing:645:11)
at ServerResponse.header (D:\backend\node_modules\express\lib\response.js:794:10)
at ServerResponse.send (D:\backend\node_modules\express\lib\response.js:174:12)
at ServerResponse.json (D:\backend\node_modules\express\lib\response.js:278:15)
at errorMiddleware (D:\backend\src\middlewares\error.middleware.ts:11:24)
at Layer.handle_error (D:\backend\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\backend\node_modules\express\lib\router\index.js:326:13)
at D:\backend\node_modules\express\lib\router\index.js:286:9 at Function.process_params
I have tried researching this issue but cannot seem to pinpoint the problem. It was running smoothly a few days ago and now encounters an error.
I am expecting a response from the Google API containing the user's access, id, and refresh tokens.