|
1 | | -import requests |
| 1 | +import json |
2 | 2 | import django |
3 | 3 | import flask |
| 4 | +import requests |
4 | 5 |
|
5 | | -AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMAAAZYX" |
6 | | -AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMAAAKEY" |
| 6 | +from google.oauth2 import service_account |
| 7 | +from google.cloud import storage |
7 | 8 |
|
| 9 | +# BAD PRACTICE: Embedding service account JSON directly in code |
| 10 | +SERVICE_ACCOUNT_KEY = """ |
| 11 | +{ |
| 12 | + "type": "service_account", |
| 13 | + "project_id": "my-demo-project", |
| 14 | + "private_key_id": "1234567890abcdef1234567890abcdef12345678", |
| 15 | + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqh...\n-----END PRIVATE KEY-----\n", |
| 16 | + "client_email": "demo-service-account@my-demo-project.iam.gserviceaccount.com", |
| 17 | + "client_id": "111111111111111111111", |
| 18 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 19 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 20 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 21 | + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/demo-service-account%40my-demo-project.iam.gserviceaccount.com" |
| 22 | +} |
| 23 | +""" |
8 | 24 |
|
9 | 25 | def main(): |
10 | | - print("hello") |
| 26 | + print("hello from GCP (insecure demo mode)") |
| 27 | + |
| 28 | + # Parse the JSON key from the string |
| 29 | + service_account_info = json.loads(SERVICE_ACCOUNT_KEY) |
| 30 | + credentials = service_account.Credentials.from_service_account_info(service_account_info) |
| 31 | + |
| 32 | + # Example: List buckets with these credentials |
| 33 | + client = storage.Client(credentials=credentials, project=service_account_info["project_id"]) |
| 34 | + |
| 35 | + print("Buckets in project", service_account_info["project_id"]) |
| 36 | + for bucket in client.list_buckets(): |
| 37 | + print(" -", bucket.name) |
11 | 38 |
|
12 | 39 |
|
13 | 40 | if __name__ == "__main__": |
14 | 41 | main() |
15 | | - |
|
0 commit comments