เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การยืนยันตัวตนด้วย API key ผ่าน requests

การยืนยันตัวตนด้วย API key ทำงานคล้ายกับ Basic Authentication แต่ต้องระบุ API key ที่ไม่ซ้ำกันผ่านทาง request header หรือ URL parameter สำหรับคำขอที่ต้องการการยืนยันตัวตน มาลองสำรวจทั้งสองวิธีกัน

ข้อมูลที่ควรทราบ:

  1. แพ็กเกจ requests ถูก import ไว้แล้ว
  2. ใช้ API key/token 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')
แก้ไขและรันโค้ด