Hello OpenAI Developer Community,
I am having trouble with the OpenAI API and am reaching out for assistance. I have implemented the API in my Python code, but am encountering a 403 error when making the request. I have double checked and confirmed that my API key is correct, but am still unable to access the API.
Here is the code that I am using:
import json
import requestsLoad the test data from the JSON file
with open(‘test_data.json’, ‘r’) as f:
data = json.load(f)Update the URL with the correct endpoint for the OpenAI API
url = “https://api.openai.com/v1/engines/text-davinci-002/jobs”
Update the headers with the correct Content-Type and API key
headers = {
‘Content-Type’: ‘application/json’,
‘Authorization’: ‘Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ABOykrcC’
}Make a post request to the OpenAI API with the data and headers
response = requests.post(url, headers=headers, json=data)
Check if the response is successful
if response.status_code == 200:
# Extract the test outline from the response
test_outline = response.json()[‘choices’][0][‘text’]# Write the test outline to a file with open('test_outline.txt', 'w') as f: f.write(test_outline)
else:
# Print an error message if the request fails
print(f’Request failed with status code: {response.status_code}')
I would greatly appreciate any help or guidance in resolving this issue. Thank you for your time and support.
Best regards,