def get_credentials(): scope = ['https://spreadsheets.google.com/feeds'] #credentials = ServiceAccountCredentials.from_json_keyfile_name('../cred.json', scope) cred_json = generate_json() credentials = ServiceAccountCredentials._from_parsed_json_keyfile( cred_json, scope) return credentials
def __init__(self, service_account_json, sub_account, calendar_id, welcome_text, welcome_attachments): """Initialize Google API Service Interface Given a service account json object and super admin account email from Google Apps Domain, this function initializes the GoogleAdmin class by creating a Google Admin SDK API service object. Args: service_account_json (dict): Parsed JSON Key File for a Google Service Account sub_account (str): The super admin account to act on behalf of calendar_id (str): The company calendar to add new users to as readers welcome_text (str): Customizable welcome email text for each new account welcome_attachments (list): Customizable list of files to attach to welcome email """ credentials = ServiceAccountCredentials._from_parsed_json_keyfile( service_account_json, SCOPES) delegated_creds = credentials.create_delegated(sub_account) http = delegated_creds.authorize(Http()) self.sub_account = sub_account self.calendar_id = calendar_id self.admin_service = build('admin', 'directory_v1', http=http) self.gmail_service = build('gmail', 'v1', http=http) self.cal_service = build('calendar', 'v3', http=http) self.primary_domain = self._get_primary_domain() self.welcome_text = welcome_text self.welcome_attachments = welcome_attachments
def setup_creds(self): creds = ServiceAccountCredentials._from_parsed_json_keyfile( settings.GOOGLE_KEYFILE_DICT, settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE ) creds = creds.create_delegated(settings.G_DELEGATED_USER) creds.get_access_token() return creds
def setup_creds(self): creds = ServiceAccountCredentials._from_parsed_json_keyfile( settings.GOOGLE_KEYFILE_DICT, settings.GOOGLE_AUTH_SCOPE, ) creds = creds.create_delegated(settings.G_DELEGATED_USER) creds.get_access_token() return creds
def build_service(name, version, credentials=None): if credentials is None: credentials = ServiceAccountCredentials._from_parsed_json_keyfile( current_app.config['GOOGLE_SERVICE_ACCOUNT'], scopes=current_app.config['GOOGLE_SERVICE_ACCOUNT_SCOPES']) http = credentials.authorize(httplib2.Http()) return build(name, version, http=http)
def download_translations_data() -> gspread.models.ValueRange: scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] creds = ServiceAccountCredentials._from_parsed_json_keyfile( CLIENT_SECRET, scope) client = gspread.authorize(creds) sheet = client.open_by_url(SHEET_URL).sheet1 return sheet.get(f'A7:G{sheet.row_count}')
def __init__(self): print("INITIALIZING NEW SPREADSHEET SERVICE...") #self.credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILEPATH, AUTH_SCOPE) self.credentials = ServiceAccountCredentials._from_parsed_json_keyfile( json.loads(GOOGLE_API_CREDENTIALS), AUTH_SCOPE) self.client = gspread.authorize( self.credentials) #> <class 'gspread.client.Client'> self.sheet_id = DOCUMENT_KEY self.sheet_name = SHEET_NAME self.sheet = None self.products = None
def get_products(): #credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILEPATH, AUTH_SCOPE) credentials = ServiceAccountCredentials._from_parsed_json_keyfile( json.loads(GOOGLE_API_CREDENTIALS), AUTH_SCOPE) client = gspread.authorize(credentials) #> <class 'gspread.client.Client'> print("GETTING PRODUCTS FROM THE SPREADSHEET...") doc = client.open_by_key( DOCUMENT_KEY) #> <class 'gspread.models.Spreadsheet'> sheet = doc.worksheet(SHEET_NAME) #> <class 'gspread.models.Worksheet'> rows = sheet.get_all_records() #> <class 'list'> return sheet, rows
def connect_to_sheet(key_json): # Authorize the API scope = [ 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file' ] creds = ServiceAccountCredentials._from_parsed_json_keyfile( key_json, scope) client = gspread.authorize(creds) return (client)
async def report(ctx, *, stopInfo): scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] credentials = ServiceAccountCredentials._from_parsed_json_keyfile( CLIENT_SECRET, scope) gc = gspread.authorize(credentials) wksheet = gc.open("QuestReport").get_worksheet(0) pstDelta = timedelta(hours=7) #8 for daylight savings, 7 for normal timeStamp = datetime.now() - pstDelta formattedTimeStamp = "{:%m/%d/%Y %I:%M%p}".format(timeStamp) stopName, stopLoc, stopReward = stopInfo.split(",") wksheet.append_row([stopName, stopLoc, stopReward, formattedTimeStamp]) await ctx.bot.add_reaction(ctx.message, emoji="✅")
def credentials(self): ''' Instantiate credentials manually instead of using pygsheets.authorize, so that we can pass everything in through the environment instead of managing JSON credentials files. ''' parsed_keyfile = { 'type': 'service_account', 'client_email': os.environ['GOOGLE_CLIENT_EMAIL'], 'private_key': os.environ['GOOGLE_PRIVATE_KEY'], 'private_key_id': os.environ['GOOGLE_PRIVATE_KEY_ID'], 'client_id': os.environ['GOOGLE_CLIENT_ID'] } return ServiceAccountCredentials._from_parsed_json_keyfile( parsed_keyfile, scopes=SCOPES)
async def find(ctx, *, query): scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] credentials = ServiceAccountCredentials._from_parsed_json_keyfile( CLIENT_SECRET, scope) gc = gspread.authorize(credentials) wksheet = gc.open("QuestReport").get_worksheet(1) last_row = len(wksheet.get_all_records()) list_of_lists = wksheet.get_all_values( ) #returns list of lists of gym items (including headers) for l in list_of_lists[1:last_row + 1]: if str(query) in l[0]: bot_message = await bot.say(l[4]) bot_message
def _generate_credentials(self): if isinstance(self.client_credentials, dict): credentials = ServiceAccountCredentials._from_parsed_json_keyfile( self.client_credentials, ['https://spreadsheets.google.com/feeds']) else: credentials = ServiceAccountCredentials.from_json_keyfile_name( self.client_credentials, ['https://spreadsheets.google.com/feeds']) gc = gspread.authorize(credentials) self.sheet = gc.open_by_key(self.params_sheet_id).sheet1 first_cell = self.sheet.cell(1, 1).value try: self.column_row_id = int(first_cell) except ValueError: self.column_row_id = 1 self.columns = self.sheet.row_values(self.column_row_id)
async def clearoldquests(ctx): scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] credentials = ServiceAccountCredentials._from_parsed_json_keyfile( CLIENT_SECRET, scope) gc = gspread.authorize(credentials) wksheet = gc.open("QuestReport").sheet1 pstDelta = timedelta(hours=7) current_time = datetime.now() - pstDelta current_time_date = current_time.date() last_row = len(wksheet.get_all_records()) for r in reversed(range(2, last_row + 2)): quest_timestamp = wksheet.cell(r, 4).value quest_date = datetime.strptime(quest_timestamp, '%m/%d/%Y %I:%M%p').date() if quest_timestamp is not None and quest_date != current_time_date: wksheet.delete_row(r) await ctx.bot.add_reaction(ctx.message, emoji="✅")
def login(self, *args, **kwargs): credentials = drive_lib.config.data.get('credentials', None) scope = drive_lib.config.data.get('scope', list()) if not credentials: LOGGER.warning("Non-valid Drive credentials found ...") return False creds = ServiceAccountCredentials._from_parsed_json_keyfile( dict(credentials), scope) try: self.__class__._client = gspread.authorize(creds) except Exception(RuntimeError, ValueError) as exc: LOGGER.error( 'Error while logging into Drive Production Tracking ...') raise (exc) spreadsheets = drive_lib.config.data.get('spreadsheets', None) if not spreadsheets: LOGGER.warning( 'No spreadsheets to open defined in Drive Production Tracking Configuration File!' ) return False for spreadsheet_name in spreadsheets: try: open_sheet = self.__class__._client.open(spreadsheet_name) self.__class__._spreadsheets.append(open_sheet) except gspread.client.SpreadsheetNotFound as exc: LOGGER.warning( 'Production Tracking Sheet Document: "{}" does not exists!' .format(spreadsheet_name)) continue if not self.__class__._spreadsheets: LOGGER.warning( 'Any of the spreadsheets defined in Drive Production Tracking Configuration File were valid!' ) return False return True
def view(params): scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] a = { "type": "service_account", "project_id": "turing-striker-162110", "private_key_id": "4f7d7dd61c3b5f779955c9208c2a8fc013679e16", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCp8xh0U2ME+jDV\nvcO5Oh5120fsL8bLhOiBTWIL77fUcie0izQY9R+iHK5QEXUpobacK7grfS4rLmYt\nD47WLycaw/J+QED17N0xXpvyy7APpO6YWRselP0iMKSYho1z+giyzXnCgUDM9OGZ\n8SVySLwNQIv49TF8h97j3Mo3A3oei8Ka4sNNnhEn4M1ay5frpT7oVlDner78A8Fj\nqIkOR3nrY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viTgH5c0fpZW4Hn4aNRlAiwtNwb\niZSmPjWRBnt8AdBIf2YQpu2MGLpHgU+HFcfR597D6PP3vK2hP21JKCWz9iEw50VI\n/oyeJL2dk5NFKWZ1rxksjWrsVuY3sl9PMq0guOECgYA/2kBFXCLcTeu4+qSGTzuv\nNwhZpyG3Y6YwTp49O3c8ofarZ38mYozL7efQUivxhoBKE+WIQhxN7bdIAlSEX/rx\nmpWIp/VOabt0vyrZBqcXBOKjRtIp9Oz+OCSBB7n5HGkwRYYSGOxPXWV04/iVLBZe\nZ0HAGFeFWKUky7y88szXqg==\n-----END PRIVATE KEY-----\n", "client_email": "*****@*****.**", "client_id": "102083397472594304607", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/starting-account-xawhri1413s4%40turing-striker-162110.iam.gserviceaccount.com" } credentials = ServiceAccountCredentials._from_parsed_json_keyfile(a, scope) gc = gspread.authorize(credentials) sh = gc.open_by_url( 'https://docs.google.com/spreadsheets/d/1k_k59oXUVaAai2auEcQ884fnemmhXdaSjPjNeMolfzE/edit?ts=5dea0f98#gid=0' ) my_list = [param for param in params] return sh.sheet1.append_row(my_list)
def get_products(): try: if server == False: credentials = ServiceAccountCredentials.from_json_keyfile_name( CREDENTIALS_FILEPATH, AUTH_SCOPE) else: credentials = ServiceAccountCredentials._from_parsed_json_keyfile( json.loads(GOOGLE_API_CREDENTIALS), AUTH_SCOPE) client = gspread.authorize( credentials) #> <class 'gspread.client.Client'> doc = client.open_by_key( DOCUMENT_KEY) #> <class 'gspread.models.Spreadsheet'> sheet = doc.worksheet( SHEET_NAME) #> <class 'gspread.models.Worksheet'> rows = sheet.get_all_records() #> <class 'list'> except TypeError: print(TypeError) rows = sheet = [] print("Error in spreadsheet.py") except Exception: print(Exception) rows = sheet = [] print("Error in spreadsheet.py") return sheet, rows
scopes = ['https://www.googleapis.com/auth/analytics.readonly'] json_data = { "type": "service_account", "project_id": "ga-reporting-", "private_key_id": "", "private_key": "", "client_email": "", "client_id": "", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token", "auth_provider_x509_cert_url": "", "client_x509_cert_url": "" } credentials = ServiceAccountCredentials._from_parsed_json_keyfile( keyfile_dict=json_data, scopes=scopes) class SampledDataError(Exception): pass date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('20%y-%m-%d') date_ranges = [(date, date)] def main(argv, metrics, dimensions, sort, profileId, profileName, writer, logger): service, flags = sample_tools.init( argv,
app = Flask(__name__) # Configure the flask app app.config.from_object("tiles.config") # initialize all of the extensions cache.init_app(app) disk = DiskCache('tile_cache', shards=app.config.get('TILE_CACHE_SHARDS'), size_limit=app.config.get('TILE_CACHE_SIZE_LIMIT'), eviction_policy=app.config.get('TILE_CACHE_EVICTION')) # initialize google earth engine ee.Initialize(ServiceAccountCredentials._from_parsed_json_keyfile( app.config['GOOGLE_SERVICE_ACCOUNT'], scopes=app.config['GOOGLE_SERVICE_ACCOUNT_SCOPES'])) def parse_request_args_values(key): """ Takes in values for query parameters and returns a single element if the length of the array is one. :param values: * :return: * """ values = request.args.getlist(key) if type(values) is list and len(values) == 1: return values[0] return values
def __init__(self,service_account): scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] self.__creds = ServiceAccountCredentials._from_parsed_json_keyfile(service_account,scope) self.__google_client = gspread.authorize(self.__creds)
driver.minimize_window() driver.refresh() time.sleep(10) elem = driver.find_element_by_xpath('x_path') #If you use Google Spreadsheets this may help you, but you will have to make an ServiceAccountCredential from Google API import gspread from oauth2client.service_account import ServiceAccountCredentials G_key = { "You could paste here the code in your client_secret.json file, otherwise you will need to leave it in the same folder than your python file. Also remember to give edit access to the e-mail in the client_secret.json file." } scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] creds = ServiceAccountCredentials._from_parsed_json_keyfile(G_key, scope) client = gspread.authorize(creds) sheet = client.open('your sheet name') worksheet = sheet.worksheet("your worksheet name") #with this you can get from the worksheet: value = worksheet.cell(row, col).value #with this you write in your worksheet: worksheet.update_cell(row, col, value) #If you use dataframes, you can use: import gspread_dataframe as gd G_key = { "You could paste here the code in your client_secret.json file, otherwise you will need to leave it in the same folder than your python file. Also remember to give edit access to the e-mail in the client_secret.json file." } scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'
def set_credentials(self): self.scope = ['https://www.googleapis.com/auth/spreadsheets.readonly'] self.service_account_info = json.loads(os.environ['Google_Key']) self.credentials = ServiceAccountCredentials._from_parsed_json_keyfile( self.service_account_info, self.scope) self.file = gspread.authorize(self.credentials)
def create_app(config='Testing'): app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app) # Configure the flask app app.config.from_object("croplands_api.config." + config) # initialize all of the extensions jwt.init_app(app) celery.init_app(app) db.init_app(app) limiter.init_app(app) cache.init_app(app) compress.init_app(app) mail.init_app(app) api.init_app(app, flask_sqlalchemy_db=db) # initialize google earth engine ee.Initialize(ServiceAccountCredentials._from_parsed_json_keyfile( app.config['GOOGLE_SERVICE_ACCOUNT'], scopes=app.config['GOOGLE_SERVICE_ACCOUNT_SCOPES'])) # import and register all of the blueprints from croplands_api.views.public import public from croplands_api.views.auth import auth from croplands_api.views.gee import gee from croplands_api.views.aws import aws from croplands_api.views.upload import upload from croplands_api.views.stats import stats_blueprint from croplands_api.views.data import data_blueprint app.register_blueprint(public) app.register_blueprint(gee) app.register_blueprint(aws) app.register_blueprint(auth) app.register_blueprint(upload) app.register_blueprint(stats_blueprint) app.register_blueprint(data_blueprint) from croplands_api.views.api import init_api init_api(app) # import and init error handlers from croplands_api.views.errors import init_error_handlers init_error_handlers(app) # cors headers and cache app.after_request(add_cors_headers) from croplands_api.auth import load_user, is_anonymous app.before_request(load_user) from croplands_api.utils.log import log app.after_request(log) @limiter.request_filter def registered(): """ Removes limit if user is registered and using a token. :return: """ return not is_anonymous() if 'POSTMARK_API_KEY' in app.config: email_handler = PostMarkHandler(api_key=app.config['POSTMARK_API_KEY']) email_handler.setLevel(logging.ERROR) app.logger.addHandler(email_handler) import croplands_api.tasks.high_res_imagery import croplands_api.tasks.classifications import croplands_api.tasks.reference_data_coverage import croplands_api.tasks.records return app