我已经用打字稿编写了这段代码,它获取
access_token
和 id_token
,以换取用户从 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);
}
}
代码工作正常,直到突然开始出现以下错误:
>> 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
我尝试研究这个问题,但我不明白这个问题。 我不明白这个问题,因为几天前它工作得很好,现在却抛出错误。
我期望来自 Google Api 的响应包含用户的访问权限、ID 和刷新令牌。