BaşlayınÜcretsiz Başlayın

Basic Authentication with requests

Basic Authentication is the simplest authentication method for web APIs. It works like logging into a website. To gain access, you need to send your personal username and password along with every request. Using this username and password, the API can identify you and grant you access to the requested data.

Let's first learn how a server responds when authentication fails, and then let's fix it by using Basic Authentication.

Good to know:

  1. The requests package has already been imported.
  2. You can use the username [email protected] and the password Warp_ExtrapolationsForfeited2 to authenticate.

Bu egzersiz

Introduction to APIs in Python

kursunun bir parçasıdır
Kursu Görüntüle

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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')
Kodu Düzenle ve Çalıştır