requests로 Basic 인증하기
Basic Authentication은 웹 API에서 가장 단순한 인증 방식이에요. 웹사이트에 로그인하는 것과 비슷하게 작동합니다. 접근하려면 매 요청마다 사용자 이름과 비밀번호를 함께 보내야 해요. API는 이 사용자 이름과 비밀번호로 여러분을 식별하고, 요청한 데이터에 대한 접근 권한을 부여합니다.
먼저 인증에 실패했을 때 서버가 어떻게 응답하는지 확인한 다음, Basic Authentication을 사용해 문제를 해결해 보겠습니다.
알아두면 좋아요:
requests패키지는 이미 임포트되어 있어요.- 인증에는 사용자 이름
[email protected]과 비밀번호Warp_ExtrapolationsForfeited2를 사용할 수 있어요.
이 연습은 강의의 일부입니다
Python으로 배우는 API 입문
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
response = requests.get('http://localhost:3000/albums')
# Check if the status code on the response object matches a successful response
if(response.____ == ____):
print("Success!")
# Check if the status code indicates a failed authentication attempt
elif(response.____ == ____):
print('Authentication failed')
else:
print('Another error occurred')