Failed to retrieve data. HTTP Status: 403
import requests import json
google_api_key = 'my api key'
url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + google_api_key
data = { "contents": [{ "parts": [{ "text": "Write a story about a magic backpack." }] }] }
headers = { 'Content-Type': 'application/json', "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200: print("Response from server:", response.json()) else: print("Failed to retrieve data. HTTP Status:", response.status_code)
What should i do to solve my problem with HTTP Status: 403?
Hi, can you provide more information and print out what the error details, if any, you see?
Hi, there's nothing wrong with your code, it works fine for me.
Are you sure your API key is good and that you're in an available region?
Marking this issue as stale since it has been open for 14 days with no activity. This issue will be closed if no further activity occurs.
This issue was closed because it has been inactive for 28 days. Please post a new issue if you need further assistance. Thanks!