शुरू करेंमुफ़्त में शुरू करें

requests के साथ API key authentication

API key-आधारित authentication, Basic Authentication की तरह ही काम करता है, लेकिन authenticated requests के लिए आपको एक यूनिक API key को या तो request header में या URL parameter के रूप में शामिल करना होता है। आइए दोनों तरीकों को देखें।

ध्यान रखने योग्य बातें:

  1. requests पैकेज पहले से इम्पोर्ट किया गया है।
  2. ऑथेन्टिकेट करने के लिए API key/token 8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3 का उपयोग करें।

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में APIs परिचय

पाठ्यक्रम देखें

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

# 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')
कोड संपादित करें और चलाएँ