안녕하세요 RESTAPI 키 때문에 문의드립니다!

카카오 모빌리티에서 https://apis-navi.kakaomobility.com/v1/directions 에서 GET요청을 할때, 카카오 디벨롭 RESTAPI 키를 사용하는걸로 알고 있는데, 사용을 하니 {
“code”: -401,
“msg”: “cannot find Authorization : KakaoAK header”
} 라고 나오네요 Header에 Authorization : KakaoAK ${REST_API_KEY} ← 여기에 발급된거 넣었음) 했는데 안되네요 왜그런걸까요?

디벨롭 아이디는 ID 1277684 입니다!

앱키가 잘못되었다는 메시지로 보입니다.

호출하신 코드블럭(헤더 설정부 포함)과 앱키일부분 블라인드 처리해서 기재 부탁드려요.

import { type NextRequest, NextResponse } from “next/server”

export async function GET(request: NextRequest) {
try {
const apiKey = process.env.KAKAO_MOBILITY_API_KEY

console.log("API 키 확인:", apiKey ? "설정됨" : "설정되지 않음")

if (!apiKey || apiKey === "your_kakao_mobility_api_key_here") {
  return NextResponse.json({
    status: "error",
    message: "API 키가 설정되지 않았습니다.",
    apiKey: apiKey || "undefined",
  })
}

// 올바른 카카오 모빌리티 API 엔드포인트로 테스트
const testResponse = await fetch("https://apis-navi.kakaomobility.com/v1/directions", {
  method: "GET",
  headers: {
    Authorization: `KakaoAK ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    origin: {
      x: 126.978,
      y: 37.566,
    },
    destination: {
      x: 127.028,
      y: 37.498,
    },
    priority: "RECOMMEND",
  }),
})

console.log("테스트 API 응답 상태:", testResponse.status)
console.log("응답 헤더:", Object.fromEntries(testResponse.headers.entries()))

if (testResponse.ok) {
  const responseData = await testResponse.json()
  return NextResponse.json({
    status: "success",
    message: "API 키가 정상적으로 작동합니다.",
    apiKeyLength: apiKey.length,
    testResult: responseData,
  })
} else {
  const errorText = await testResponse.text()
  console.error("API 오류 응답:", errorText)

  return NextResponse.json({
    status: "error",
    message: "API 키가 유효하지 않거나 API 호출에 실패했습니다.",
    details: errorText,
    statusCode: testResponse.status,
    url: testResponse.url,
  })
}

} catch (error) {
console.error(“API 키 테스트 오류:”, error)
const errorMessage = error instanceof Error ? error.message : “알 수 없는 오류가 발생했습니다.”
return NextResponse.json({
status: “error”,
message: “API 키 테스트 중 오류가 발생했습니다.”,
error: errorMessage,
})
}
}
이런식으로 했습니다

키는 be818e812c08b93832cfd0xxxxxxxx 사용했습니다.

우선, 1277684앱 로그상 구동되는 환경의 도메인을 등록하지 않아 발생한 지도 에러가 다수 확인됩니다.

{"code":-401,"msg":"domain mismatched! caller=https://preview-kakao-maps-api-integration-kzmqex0yvoski43q7h3q.vusercontent.net. check out registered web domains."}

기재하신 코드를 보니 정상작동하지 않을 것 같은데요.
method: “GET”,인데 body: JSON 사용하셨네요?
앱키와 무관하게 먼저 수정해보시겠어요?

https://developers.kakaomobility.com/docs/navi-api/directions/