-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify_api_key.py
45 lines (35 loc) · 1.27 KB
/
verify_api_key.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
import argparse
import requests
def verify_api_key(api_key):
# Define the API endpoint URL
api_url = "https://api.openai.com/v1/engines/davinci/completions"
# Set up the headers with the API key
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Create a test request to check the API key
try:
response = requests.post(api_url, headers=headers)
response.raise_for_status() # Raise an exception if the request fails
# If the request is successful, the API key is valid
return True
except requests.exceptions.RequestException:
# If the request fails, the API key is invalid
return False
def main():
# Create a command-line argument parser
parser = argparse.ArgumentParser(description="Verify OpenAI API keys")
# Add an argument for the API key
parser.add_argument("api_key", type=str, help="The API key to verify")
# Parse the command-line arguments
args = parser.parse_args()
# Get the API key from the parsed arguments
api_key = args.api_key
# Verify the API key
if verify_api_key(api_key):
print("API key is valid.")
else:
print("API key is invalid.")
if __name__ == "__main__":
main()