Last active
April 10, 2021 00:52
-
-
Save upodroid/b31b5e42d9b3fa2caab8d605335a161a to your computer and use it in GitHub Desktop.
Google Service Account Impersonation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import googleapiclient.errors | |
import requests | |
from google.oauth2.credentials import Credentials | |
from googleapiclient.discovery import build | |
def GetCredentials(client_id, client_secret, refresh_token): | |
"""Convert ADC to access_token/Credential""" | |
params = { | |
"grant_type": "refresh_token", | |
"client_id": client_id, | |
"client_secret": client_secret, | |
"refresh_token": refresh_token | |
} | |
authorization_url = "https://www.googleapis.com/oauth2/v4/token" | |
r = requests.post(authorization_url, data=params) | |
if r.ok: | |
credentials = Credentials(token=r.json()['access_token']) | |
return credentials | |
else: | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import googleapiclient.errors | |
from google.oauth2.credentials import Credentials | |
from googleapiclient.discovery import build | |
# https://stackoverflow.com/questions/54321848/how-to-use-refresh-token-google-api-in-python ADC Token | |
def Impersonate(lifetime, targetServiceAccount) | |
"""Service Account Impersonation""" | |
try: | |
creds = build('iamcredentials', 'v1', cache_discovery=False).projects().serviceAccounts().generateAccessToken( | |
name='projects/-/serviceAccounts/'+targetServiceAccount, | |
body = { | |
'lifetime': lifetime+'s', | |
'scope': ['https://www.googleapis.com/auth/cloud-platform'] | |
}).execute() | |
except googleapiclient.errors.HttpError as e: | |
print(e) | |
pass | |
credentials = Credentials(token=creds['accessToken']) | |
return credentials |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://markhneedham.com/blog/2015/11/28/python-parsing-a-json-http-chunking-stream/ | |
from google.cloud import pubsub_v1 | |
from google.api_core import exceptions | |
import os | |
import requests | |
import json | |
topic_name = os.getenv('TOPIC_NAME') | |
project_id = os.getenv('PROJECT_ID') | |
def PublishToPubSub(topic_name, payload): | |
publisher = pubsub_v1.PublisherClient() | |
try: | |
publisher.publish( | |
topic = 'projects/'+project_id+'/topics/'+topic_name, | |
data = payload.encode("utf-8") | |
) | |
except exceptions.ClientError as e: | |
print(e) | |
dummy_json = requests.get('https://jsonplaceholder.typicode.com/todos/1').json() | |
PublishToPubSub(topic_name, json.dumps(dummy_json)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment