การยืนยันตัวตนด้วย API key ผ่าน requests
การยืนยันตัวตนด้วย API key ทำงานคล้ายกับ Basic Authentication แต่ต้องระบุ API key ที่ไม่ซ้ำกันผ่านทาง request header หรือ URL parameter สำหรับคำขอที่ต้องการการยืนยันตัวตน มาลองสำรวจทั้งสองวิธีกัน
ข้อมูลที่ควรทราบ:
- แพ็กเกจ
requestsถูก import ไว้แล้ว - ใช้ 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')