requests के साथ Basic Authentication
Basic Authentication वेब APIs के लिए सबसे सरल authentication विधि है. यह किसी वेबसाइट में लॉगिन करने जैसा ही काम करती है. एक्सेस पाने के लिए, आपको हर रिक्वेस्ट के साथ अपना व्यक्तिगत username और password भेजना होता है. इस username और password का उपयोग करके API आपकी पहचान कर सकती है और अनुरोधित डेटा तक आपको पहुँच दे सकती है.
पहले सीखते हैं कि authentication फेल होने पर सर्वर कैसी प्रतिक्रिया देता है, और फिर इसे Basic Authentication का उपयोग करके ठीक करते हैं.
ध्यान रखने योग्य बातें:
requestsपैकेज पहले से इम्पोर्ट किया गया है.- आप authenticate करने के लिए username
[email protected]और passwordWarp_ExtrapolationsForfeited2का उपयोग कर सकते हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में APIs परिचय
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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')