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
|
credentials/
|
||||||
__pycache__/
|
|
||||||
*.py[oc]
|
|
||||||
build/
|
|
||||||
dist/
|
|
||||||
wheels/
|
|
||||||
*.egg-info
|
|
||||||
|
|
||||||
# Virtual environments
|
|
||||||
.venv
|
|
||||||
|
|
||||||
#
|
|
||||||
*.key
|
*.key
|
||||||
credentials.json
|
*.json
|
||||||
token.json
|
|
||||||
75
main.py
75
main.py
@@ -7,6 +7,13 @@ from google.api_core import exceptions as google_exceptions
|
|||||||
from google.oauth2 import id_token
|
from google.oauth2 import id_token
|
||||||
from google.auth.transport import requests
|
from google.auth.transport import requests
|
||||||
|
|
||||||
|
# --- CONFIGURATION ---
|
||||||
|
EMAILS = [
|
||||||
|
"aetheriumglimmer"
|
||||||
|
]
|
||||||
|
CREDENTIALS_DIR = "credentials"
|
||||||
|
# ---------------------
|
||||||
|
|
||||||
SCOPES = [
|
SCOPES = [
|
||||||
"https://www.googleapis.com/auth/cloud-platform",
|
"https://www.googleapis.com/auth/cloud-platform",
|
||||||
"https://www.googleapis.com/auth/userinfo.email",
|
"https://www.googleapis.com/auth/userinfo.email",
|
||||||
@@ -14,41 +21,49 @@ SCOPES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
def main():
|
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
|
creds = None
|
||||||
if os.path.exists("token.json"):
|
if os.path.exists(token_file):
|
||||||
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
|
creds = Credentials.from_authorized_user_file(token_file, SCOPES)
|
||||||
|
|
||||||
if not creds or not creds.valid:
|
if not creds or not creds.valid:
|
||||||
if creds and creds.expired and creds.refresh_token:
|
if creds and creds.expired and creds.refresh_token:
|
||||||
creds.refresh(google.auth.transport.requests.Request())
|
creds.refresh(google.auth.transport.requests.Request())
|
||||||
else:
|
else:
|
||||||
|
print(f"Please authenticate with: {email}")
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
flow = InstalledAppFlow.from_client_secrets_file(
|
||||||
"credentials.json", SCOPES
|
"credentials.json", SCOPES
|
||||||
)
|
)
|
||||||
creds = flow.run_local_server(port=0)
|
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())
|
token.write(creds.to_json())
|
||||||
|
return creds
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def get_user_email(credentials):
|
def get_user_email(credentials):
|
||||||
try:
|
try:
|
||||||
@@ -61,7 +76,6 @@ def get_user_email(credentials):
|
|||||||
print(f"Error getting user email: {err}")
|
print(f"Error getting user email: {err}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def enable_api(project_id, credentials):
|
def enable_api(project_id, credentials):
|
||||||
try:
|
try:
|
||||||
service_usage_client = service_usage_v1.ServiceUsageClient(credentials=credentials)
|
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}"
|
name=f"projects/{project_id}/services/{service_name}"
|
||||||
)
|
)
|
||||||
operation = service_usage_client.enable_service(request=request)
|
operation = service_usage_client.enable_service(request=request)
|
||||||
# Wait for the operation to complete
|
operation.result() # Wait for the operation to complete
|
||||||
operation.result()
|
|
||||||
print(f"Enabled Generative Language API for project {project_id}")
|
print(f"Enabled Generative Language API for project {project_id}")
|
||||||
except google_exceptions.GoogleAPICallError as err:
|
except google_exceptions.GoogleAPICallError as err:
|
||||||
print(f"Error enabling API for project {project_id}: {err}")
|
print(f"Error enabling API for project {project_id}: {err}")
|
||||||
|
|
||||||
|
|
||||||
def create_api_key(project_id, credentials):
|
def create_api_key(project_id, credentials):
|
||||||
try:
|
try:
|
||||||
api_keys_client = api_keys_v2.ApiKeysClient(credentials=credentials)
|
api_keys_client = api_keys_v2.ApiKeysClient(credentials=credentials)
|
||||||
@@ -88,19 +100,16 @@ def create_api_key(project_id, credentials):
|
|||||||
key=key,
|
key=key,
|
||||||
)
|
)
|
||||||
operation = api_keys_client.create_key(request=request)
|
operation = api_keys_client.create_key(request=request)
|
||||||
# Wait for the operation to complete
|
result = operation.result() # Wait for the operation to complete
|
||||||
result = operation.result()
|
|
||||||
print(f"Created API key for project {project_id}")
|
print(f"Created API key for project {project_id}")
|
||||||
return result
|
return result
|
||||||
except google_exceptions.GoogleAPICallError as err:
|
except google_exceptions.GoogleAPICallError as err:
|
||||||
print(f"Error creating API key for project {project_id}: {err}")
|
print(f"Error creating API key for project {project_id}: {err}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def save_api_key(email, api_key):
|
def save_api_key(email, api_key):
|
||||||
with open(f"{email}.key", "a") as f:
|
with open(f"{email}.key", "a") as f:
|
||||||
f.write(f"{api_key}\n")
|
f.write(f"{api_key}\n")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user