yarn add axios
yarn add @nestjs/axios
: 요청하는 access token은 네이버 유저 정보를 요청하기 위한 토큰이다.
async getNaverAccessToken(code: string): Promise<string> {
const url = '<https://nid.naver.com/oauth2.0/token>';
const clientId = this.configService.get('NAVER_CLIENT_ID');
const clientSecret = this.configService.get('NAVER_CLIENT_SECRET');
try {
return (
await this.httpService.axiosRef.post(
url,
{},
{
params: {
grant_type: 'authorization_code',
client_id: clientId,
client_secret: clientSecret,
code: code,
},
headers: { 'X-Naver-Client-Id': clientId, 'X-Naver-Client-Secret': clientSecret },
}
)
).data.access_token;
} catch (error) {
throw new HttpException('Failed to get Naver access token', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async getNaverUser(accessNaverToken: string): Promise<UserNaverDto> {
try {
return (
await this.httpService.axiosRef.post(
'<https://openapi.naver.com/v1/nid/me>',
{},
{ headers: { Authorization: `Bearer ${accessNaverToken}` } }
)
).data.response;
} catch (error) {
throw new HttpException('Failed to get Naver user data.', HttpStatus.INTERNAL_SERVER_ERROR);
}
}