시작하기무료로 시작하기

requests로 API 키 인증하기

API 키 기반 인증은 Basic 인증과 비슷하지만, 인증된 요청에는 고유한 API 키를 요청 헤더나 URL 매개변수로 포함해야 합니다. 두 가지 방식을 모두 살펴보겠습니다.

알아두면 좋아요:

  1. requests 패키지는 이미 임포트되어 있습니다.
  2. 인증에는 API 키/토큰 8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3을(를) 사용하세요.

이 연습은 강의의 일부입니다

Python으로 배우는 API 입문

강의 보기

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Create a dictionary containing the API key using the correct key-value combination
params = {____: ____}
# Add the dictionary to the requests.get() call using the correct function argument
response = requests.get('http://localhost:3000/albums', ____=params)

if(response.status_code == 200):
    print("Success!")
elif(response.status_code == 401):
    print('Authentication failed')
else:
    print('Another error occurred')
코드 편집 및 실행