-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_with_authentication.pu
32 lines (26 loc) · 1.04 KB
/
api_with_authentication.pu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Imagine you have an API endpoint that requires authentication using a token.
#Write a Python function that takes a token as input and makes a GET request
#to "https://api.example.com/data" with the token included in the request header.
#The function should handle authentication errors and return the response from the server.
#!/usr/env python3
import requests
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('auth_token', type=str, help='Authorization token for API endpoint', nargs='?', default='guest_token')
args = parser.parse_args()
token = args.auth_token
def auth_request(url, token):
headers = {
'Authorization': token
}
response = requests.get(url,headers=headers)
return response
url = "https://api.example.com/data"
response = auth_request(url, token)
try:
if response.status_code == 200:
print('Authorization Successful')
else:
print('Unauthorized, please check token')
except requests.exceptions.RequestException as e:
print(f'Error has occured during the request: {e}')