Skip to content

Instantly share code, notes, and snippets.

@jdyke
Created June 1, 2020 20:17
Show Gist options
  • Save jdyke/5e40f06e3ac0520dda6c2856ab870c7f to your computer and use it in GitHub Desktop.
Save jdyke/5e40f06e3ac0520dda6c2856ab870c7f to your computer and use it in GitHub Desktop.
get_keys
def get_keys():
access_token = create_token()
service = create_service()
# List available projects
request = service.projects().list()
response = request.execute()
# This variable is used to hold our key JSON objects before we write to key_dump.json
content = []
# For each project, extract the project ID
for project in response.get('projects', []):
project_id = project['projectId']
# Use the project ID and access token to find the API keys for each project
keys = requests.get(
f'https://apikeys.googleapis.com/v1/projects/{project_id}/apiKeys',
params={'access_token': access_token}
).json()
# Write Keys to a file for conversion
if "error" not in keys: # Removes 403 permission errors from returning
if keys:
print(f"API Key found in project ID {project_id}.")
content.append(keys['keys'])
else:
print(f"Project ID {project_id} has no API Keys.")
return content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment