mirror of
https://github.com/not-lucky/GeminiKeyManagement.git
synced 2025-12-06 08:44:01 +05:30
QOL feature; only have to login in an account once
This commit is contained in:
16
.gitignore
vendored
16
.gitignore
vendored
@@ -1,15 +1,3 @@
|
||||
# Python-generated files
|
||||
__pycache__/
|
||||
*.py[oc]
|
||||
build/
|
||||
dist/
|
||||
wheels/
|
||||
*.egg-info
|
||||
|
||||
# Virtual environments
|
||||
.venv
|
||||
|
||||
#
|
||||
credentials/
|
||||
*.key
|
||||
credentials.json
|
||||
token.json
|
||||
*.json
|
||||
73
main.py
73
main.py
@@ -7,6 +7,13 @@ from google.api_core import exceptions as google_exceptions
|
||||
from google.oauth2 import id_token
|
||||
from google.auth.transport import requests
|
||||
|
||||
# --- CONFIGURATION ---
|
||||
EMAILS = [
|
||||
"aetheriumglimmer"
|
||||
]
|
||||
CREDENTIALS_DIR = "credentials"
|
||||
# ---------------------
|
||||
|
||||
SCOPES = [
|
||||
"https://www.googleapis.com/auth/cloud-platform",
|
||||
"https://www.googleapis.com/auth/userinfo.email",
|
||||
@@ -14,41 +21,49 @@ SCOPES = [
|
||||
]
|
||||
|
||||
def main():
|
||||
if not os.path.exists(CREDENTIALS_DIR):
|
||||
os.makedirs(CREDENTIALS_DIR)
|
||||
|
||||
for email in EMAILS:
|
||||
print(f"--- Processing account: {email} ---")
|
||||
creds = get_credentials_for_email(email)
|
||||
if not creds:
|
||||
continue
|
||||
|
||||
try:
|
||||
resource_manager = resourcemanager_v3.ProjectsClient(credentials=creds)
|
||||
projects = resource_manager.search_projects()
|
||||
|
||||
print("Processing projects...")
|
||||
for project in projects:
|
||||
project_id = project.project_id
|
||||
print(f"- {project_id}")
|
||||
enable_api(project_id, creds)
|
||||
key = create_api_key(project_id, creds)
|
||||
if key:
|
||||
save_api_key(email, key.key_string)
|
||||
|
||||
except google_exceptions.GoogleAPICallError as err:
|
||||
print(f"An error occurred while processing account {email}: {err}")
|
||||
|
||||
def get_credentials_for_email(email):
|
||||
token_file = os.path.join(CREDENTIALS_DIR, f"{email}.json")
|
||||
creds = None
|
||||
if os.path.exists("token.json"):
|
||||
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
|
||||
if os.path.exists(token_file):
|
||||
creds = Credentials.from_authorized_user_file(token_file, SCOPES)
|
||||
|
||||
if not creds or not creds.valid:
|
||||
if creds and creds.expired and creds.refresh_token:
|
||||
creds.refresh(google.auth.transport.requests.Request())
|
||||
else:
|
||||
print(f"Please authenticate with: {email}")
|
||||
flow = InstalledAppFlow.from_client_secrets_file(
|
||||
"credentials.json", SCOPES
|
||||
)
|
||||
creds = flow.run_local_server(port=0)
|
||||
with open("token.json", "w") as token:
|
||||
with open(token_file, "w") as token:
|
||||
token.write(creds.to_json())
|
||||
|
||||
try:
|
||||
email = get_user_email(creds)
|
||||
if not email:
|
||||
return
|
||||
|
||||
resource_manager = resourcemanager_v3.ProjectsClient(credentials=creds)
|
||||
projects = resource_manager.search_projects()
|
||||
|
||||
print("Processing projects...")
|
||||
for project in projects:
|
||||
project_id = project.project_id
|
||||
print(f"- {project_id}")
|
||||
enable_api(project_id, creds)
|
||||
key = create_api_key(project_id, creds)
|
||||
if key:
|
||||
save_api_key(email, key.key_string)
|
||||
|
||||
except google_exceptions.GoogleAPICallError as err:
|
||||
print(err)
|
||||
|
||||
return creds
|
||||
|
||||
def get_user_email(credentials):
|
||||
try:
|
||||
@@ -61,7 +76,6 @@ def get_user_email(credentials):
|
||||
print(f"Error getting user email: {err}")
|
||||
return None
|
||||
|
||||
|
||||
def enable_api(project_id, credentials):
|
||||
try:
|
||||
service_usage_client = service_usage_v1.ServiceUsageClient(credentials=credentials)
|
||||
@@ -70,13 +84,11 @@ def enable_api(project_id, credentials):
|
||||
name=f"projects/{project_id}/services/{service_name}"
|
||||
)
|
||||
operation = service_usage_client.enable_service(request=request)
|
||||
# Wait for the operation to complete
|
||||
operation.result()
|
||||
operation.result() # Wait for the operation to complete
|
||||
print(f"Enabled Generative Language API for project {project_id}")
|
||||
except google_exceptions.GoogleAPICallError as err:
|
||||
print(f"Error enabling API for project {project_id}: {err}")
|
||||
|
||||
|
||||
def create_api_key(project_id, credentials):
|
||||
try:
|
||||
api_keys_client = api_keys_v2.ApiKeysClient(credentials=credentials)
|
||||
@@ -88,19 +100,16 @@ def create_api_key(project_id, credentials):
|
||||
key=key,
|
||||
)
|
||||
operation = api_keys_client.create_key(request=request)
|
||||
# Wait for the operation to complete
|
||||
result = operation.result()
|
||||
result = operation.result() # Wait for the operation to complete
|
||||
print(f"Created API key for project {project_id}")
|
||||
return result
|
||||
except google_exceptions.GoogleAPICallError as err:
|
||||
print(f"Error creating API key for project {project_id}: {err}")
|
||||
return None
|
||||
|
||||
|
||||
def save_api_key(email, api_key):
|
||||
with open(f"{email}.key", "a") as f:
|
||||
f.write(f"{api_key}\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user