async def close_forms(forms_list): """ To lock google forms after deadline """ global creds if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) request = { 'function': 'closeForms', 'parameters': [forms_list] } loop = asyncio.get_event_loop() http = _auth.authorized_http(creds) partialfunction = partial( service.scripts().run( body=request, scriptId=f'{GOOGLE_APP_SCRIPT}' ).execute, http=http ) response = await loop.run_in_executor(ThreadPoolExecutor(), partialfunction)
def test_authorized_http(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) http = _auth.authorized_http(credentials) self.assertIsInstance(http, google_auth_httplib2.AuthorizedHttp) self.assertEqual(http.credentials, credentials)
async def create_form(title, email): """ To create google form by running the Google Apps Script """ global creds if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) request = { 'function': 'createForm', 'parameters': [title, email] } loop = asyncio.get_event_loop() http = _auth.authorized_http(creds) partialfunction = partial( service.scripts().run( body=request, scriptId=f'{GOOGLE_APP_SCRIPT}' ).execute, http=http ) response = await loop.run_in_executor(ThreadPoolExecutor(), partialfunction) return response['response']['result']
def get_mime_message(self, msg_id): service, user_id = self.service, self.user_id try: message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute(_auth.authorized_http(self.creds)) if message is None: raise NonexistenceEmailError(f"Email {msg_id} doesn't exist") print('Message snippet: %s' % message['snippet']) msg_str = base64.urlsafe_b64decode( message['raw'].encode("utf-8")).decode("utf-8") mime_msg = email.message_from_string(msg_str) # Parse into html, text, and other parts html = '' text = '' if mime_msg.is_multipart(): for part in mime_msg.walk(): if part.get_content_type() == 'text/plain' or part.get_content_type() == 'text/html': parsed = part.get_payload(decode=True).decode('utf-8') if part.get_content_type() == 'text/plain': text = parsed if part.get_content_type() == 'text/html': html = parsed part.set_payload("Please find separate parsed file.") if part.get_content_disposition(): part.set_payload("Please find attachment in the email folder.") else: text = mime_msg.get_payload() mime_msg.set_payload("Please find separate parsed file.") return mime_msg, html, text except googleapiclient.errors.HttpError as http_error: raise NonexistenceEmailError(f"Email {msg_id} doesn't exist") except Exception as error: print('An error occurred: %s' % error)
def test_authorized_http(self): credentials = mock.Mock(spec=oauth2client.client.Credentials) http = _auth.authorized_http(credentials) self.assertEqual(http, credentials.authorize.return_value) self.assertIsInstance(credentials.authorize.call_args[0][0], httplib2.Http)
def factory(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) authorized_http = _auth.authorized_http(credentials) service = discovery.build('gmail', 'v1', http=authorized_http, cache_discovery=False) return GmailParser(service)
def start_autoupdate_service(self): request = { 'labelIds': ['INBOX'], 'topicName': self.topic } response = self.service.users().watch(userId='me', body=request).execute(_auth.authorized_http(self.creds)) self.historyId = response['historyId'] print('Starting History ID: {}'.format(self.historyId))
def get_http_client(args): """Builds and returns authorized http client.""" # Create a credential using Google Developer Service Account Credential and # Backstory API Scope. credentials = service_account.Credentials.from_service_account_file( args.credentials_file, scopes=AUTHORIZATION_SCOPES) # Build a Http client that can make authorized OAuth requests. http_client = _auth.authorized_http(credentials) return http_client
def __init__(self, refresh_token: str, token_uri: str, client_id: str, client_secret: str, owner_email: str): self.creds: Credentials = Credentials(None, refresh_token=refresh_token, token_uri=token_uri, client_id=client_id, client_secret=client_secret) self.user_id: str = owner_email self.service: Optional[Resource] = None self.http = _auth.authorized_http(self.creds)
def test_authorized_http(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) authorized_http = _auth.authorized_http(credentials) self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) self.assertEqual(authorized_http.credentials, credentials) self.assertIsInstance(authorized_http.http, httplib2.Http) self.assertIsInstance(authorized_http.http.timeout, int) self.assertGreater(authorized_http.http.timeout, 0)
def test_authorized_http(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) authorized_http = _auth.authorized_http(credentials) self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) self.assertEqual(authorized_http.credentials, credentials) self.assertIsInstance(authorized_http.http, httplib2.Http) self.assertIsInstance(authorized_http.http.timeout, int) self.assertGreater(authorized_http.http.timeout, 0)
def test_authorized_http(self): credentials = mock.Mock(spec=oauth2client.client.Credentials) authorized_http = _auth.authorized_http(credentials) http = credentials.authorize.call_args[0][0] self.assertEqual(authorized_http, credentials.authorize.return_value) self.assertIsInstance(http, httplib2.Http) self.assertIsInstance(http.timeout, int) self.assertGreater(http.timeout, 0)
def test_authorized_http(self): credentials = mock.Mock(spec=oauth2client.client.Credentials) authorized_http = _auth.authorized_http(credentials) http = credentials.authorize.call_args[0][0] self.assertEqual(authorized_http, credentials.authorize.return_value) self.assertIsInstance(http, httplib2.Http) self.assertIsInstance(http.timeout, int) self.assertGreater(http.timeout, 0)
def partial_sync(self, startHistoryId=None): # notifies of all changes after given historyId if(startHistoryId is None): startHistoryId = self.historyId def parse(history, changetype, function): """ history: Google API history object changetype: history changetype requested function: function that all relevant message objects should be passed to """ if changetype in history: print(str(changetype)) for msg in history[changetype]: print(msg['message']['id']) function(msg) def lru_add(msg): if('INBOX' in msg['message']['labelIds']): print('added to inbox') self.gmailfs.lru.add_new_email(msg['message']['id']) def lru_remove(msg): if('INBOX' in msg['message']['labelIds']): print('removed from inbox') self.gmailfs.lru.delete_message(msg['message']['id']) def isMovetoInbox(msg): if('INBOX' in msg['labelIds']): print('added to inbox') self.gmailfs.lru.add_new_email(msg['message']['id']) def isRemoveFromInbox(msg): if('INBOX' in msg['labelIds']): print('removed from inbox') self.gmailfs.lru.delete_message(msg['message']['id']) try: histories = self.service.users().history()\ .list(userId='me', startHistoryId=startHistoryId).execute(_auth.authorized_http(self.creds)) if(histories and 'history' in histories): histories = histories['history'] for history in histories: #print("NH ------------------------") #print(history) parse(history, 'messagesAdded', lru_add) parse(history, 'messagesDeleted', lru_remove) parse(history, 'labelsAdded', isMovetoInbox) parse(history, 'labelsRemoved', isRemoveFromInbox) except Exception as error: print('An error occurred during autoupdate: %s' % error) print(traceback.print_exc())
def test_authorized_http(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) authorized_http = _auth.authorized_http(credentials) self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) self.assertEqual(authorized_http.credentials, credentials) self.assertIsInstance(authorized_http.http, httplib2.Http) self.assertIsInstance(authorized_http.http.timeout, int) # Check if the Http object's timeout is set to the default value. self.assertEqual(authorized_http.http.timeout, 60)
def course_list(self, teacherEmail): cred = self.__mentor_cred http = _auth.authorized_http(cred) cred.refresh(http._request) url = "https://classroom.googleapis.com/v1/courses?courseStates=ACTIVE&teacherId=" + teacherEmail headers = { 'Accept': 'application/json', } with requests.Session() as s: s.auth = OAuth2BearerToken(cred.token) r = s.get(url, headers=headers) return r.text
def __init__(self, credentials, output_dir): self._services = {} self._zones = [] self._regions = [] self._creds = credentials self._authorized_http = authorized_http(self._creds) self._resources = {} self._all_links = set() self._output_dir = output_dir self._organization_id = None self._projects = [] self._folders = [] self._datasets = []
def get_chronicle_http_client(account_info): """ Return an http client that is authorized with the given credentials using oauth2client or google-auth. """ try: credentials = service_account.Credentials.from_service_account_info( account_info, scopes=current_app.config['AUTH_SCOPES'] ) except ValueError as e: raise AuthorizationError(str(e)) return _auth.authorized_http(credentials)
def __init__(self, app): project_id = app.project_id if not project_id: raise ValueError( 'Project ID is required to access Cloud Messaging service. Either set the ' 'projectId option, or use service account credentials. Alternatively, set the ' 'GOOGLE_CLOUD_PROJECT environment variable.') self._fcm_url = _MessagingService.FCM_URL.format(project_id) self._fcm_headers = { 'X-GOOG-API-FORMAT-VERSION': '2', 'X-FIREBASE-CLIENT': 'fire-admin-python/{0}'.format(firebase_admin.__version__), } self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential()) self._timeout = app.options.get('httpTimeout') self._transport = _auth.authorized_http(app.credential.get_credential())
def test_authorized_http(self): credentials = mock.Mock(spec=oauth2client.client.Credentials) # Overriding the default timeout value of the Http object. socket.setdefaulttimeout(42.0) http = build_http() authorized_http = _auth.authorized_http(credentials, http) http = credentials.authorize.call_args[0][0] self.assertEqual(authorized_http, credentials.authorize.return_value) self.assertIsInstance(http, httplib2.Http) self.assertIsInstance(http.timeout, float) # Check if the Http object's timeout is set to the override value. self.assertEqual(http.timeout, 42.0)
def account_create(self, firstName, lastName, personalEmail): http = _auth.authorized_http(self.__webdev_cred) self.__webdev_cred.refresh(http._request) url = "https://www.googleapis.com/admin/directory/v1/users" headers = { # 'Authorization': 'Bearer' delegated_credentials.token, 'Accept': 'application/json', 'Content-Type': 'application/json' } # checking if the email id already exists, adds an id to the end to differentiate addedID = 0 # on repeat, email will start from [email protected] def userExists(email): url = 'https://www.googleapis.com/admin/directory/v1/users/' + email with requests.Session() as s: s.auth = OAuth2BearerToken(self.__webdev_cred.token) r = s.get(url) if (r.status_code == 404): return False return True primaryEmail = firstName + '.' + lastName + '@villagementors.org' while (userExists(primaryEmail)): addedID += 1 primaryEmail = firstName + '.' + lastName + \ str(addedID) + '@villagementors.org' pwd = 'VBB' + random.choice(['!', '@', '#', '$', '%', '&']) + \ str(random.randint(100000000, 1000000000)) data = ''' { "primaryEmail": "%s", "name": { "familyName": "%s", "givenName": "%s" }, "password": "******", "changePasswordAtNextLogin": "******", "recoveryEmail": "%s", } ''' % (primaryEmail, lastName, firstName, pwd, personalEmail) with requests.Session() as s: s.auth = OAuth2BearerToken(self.__webdev_cred.token) r = s.post(url, headers=headers, data=data) return (primaryEmail, pwd)
def group_subscribe(self, groupEmail, userEmail): http = _auth.authorized_http(self.__webdev_cred) self.__webdev_cred.refresh(http._request) url = "https://www.googleapis.com/admin/directory/v1/groups/" + groupEmail + "/members" headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } data = ''' { "email": "%s", "role": "MEMBER", } ''' % (userEmail) with requests.Session() as s: s.auth = OAuth2BearerToken(self.__webdev_cred.token) r = s.post(url, headers=headers, data=data)
def get_authenticated_service(auth_root): auth_name = os.path.join(auth_root, AUTHENTICATED_SERVICE_FILE) secrets_name = os.path.join(auth_root, CLIENT_SECRETS_FILE) if os.path.isfile(auth_name): credentials = read_credentials(auth_name) else: flow = InstalledAppFlow.from_client_secrets_file(secrets_name, SCOPES) credentials = flow.run_console() write_credentials(auth_name, credentials) # The credentials need to be scoped. credentials = _auth.with_scopes(credentials, SCOPES) # Create an authorized http instance http = _auth.authorized_http(credentials) # return build(API_SERVICE_NAME, API_VERSION, credentials=credentials) return build(API_SERVICE_NAME, API_VERSION, http=http)
def _create_authenticated_http_client(self): """Create the http client using local credential file storing oauth json.""" # Constants scopes = ["https://www.googleapis.com/auth/chronicle-backstory"] # Or the location you placed your JSON file. service_account_file = os.path.abspath( os.path.join(os.environ["HOME"], "bk_credentials.json")) if not os.path.exists(service_account_file): raise ValueError("missing service account file: %s" % service_account_file) # Create a credential using Google Developer Service Account Credential and # Backstory API Scope. credentials = service_account.Credentials.from_service_account_file( service_account_file, scopes=scopes) # Build a Http client that can make authorized OAuth requests. return _auth.authorized_http(credentials)
def get_attachments(self, msg_id, store_dir): service, user_id = self.service, self.user_id # try: message = service.users().messages().get(userId=user_id, id=msg_id).execute(_auth.authorized_http(self.creds)) if message is None or 'parts' not in message['payload']: return # raise NonexistenceEmailError("Email attachments don't exist") for part in message['payload']['parts']: if (part['filename'] and part['body'] and part['body']['attachmentId']): attachment = service.users().messages().attachments().get( id=part['body']['attachmentId'], userId=user_id, messageId=msg_id).execute(_auth.authorized_http(self.creds)) file_data = base64.urlsafe_b64decode( attachment['data'].encode('utf-8')) path = ''.join([store_dir, part['filename']]) f = open(path, 'wb') f.write(file_data) f.close()
def classroom_invite(self, courseID, email, role="TEACHER"): cred = self.__mentor_cred http = _auth.authorized_http(cred) cred.refresh(http._request) url = "https://classroom.googleapis.com/v1/invitations" headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } data = ''' { "userId": "%s", "courseId": "%s", "role": "%s", } ''' % (email, courseID, role) with requests.Session() as s: s.auth = OAuth2BearerToken(cred.token) r = s.post(url, headers=headers, data=data)
def build_from_document(service, base=None, future=None, http=None, developerKey=None, model=None, requestBuilder=HttpRequest, credentials=None): """Create a Resource for interacting with an API. Same as `build()`, but constructs the Resource object from a discovery document that is it given, as opposed to retrieving one over HTTP. Args: service: string or object, the JSON discovery document describing the API. The value passed in may either be the JSON string or the deserialized JSON. base: string, base URI for all HTTP requests, usually the discovery URI. This parameter is no longer used as rootUrl and servicePath are included within the discovery document. (deprecated) future: string, discovery document with future capabilities (deprecated). http: httplib2.Http, An instance of httplib2.Http or something that acts like it that HTTP requests will be made through. developerKey: string, Key for controlling API usage, generated from the API Console. model: Model class instance that serializes and de-serializes requests and responses. requestBuilder: Takes an http request and packages it up to be executed. credentials: oauth2client.Credentials or google.auth.credentials.Credentials, credentials to be used for authentication. Returns: A Resource object with methods for interacting with the service. """ if http is not None and credentials is not None: raise ValueError( 'Arguments http and credentials are mutually exclusive.') if isinstance(service, six.string_types): service = json.loads(service) if 'rootUrl' not in service and (isinstance(http, (HttpMock, HttpMockSequence))): logger.error( "You are using HttpMock or HttpMockSequence without" + "having the service discovery doc in cache. Try calling " + "build() without mocking once first to populate the " + "cache.") raise InvalidJsonError() base = urljoin(service['rootUrl'], service['servicePath']) schema = Schemas(service) # If the http client is not specified, then we must construct an http client # to make requests. If the service has scopes, then we also need to setup # authentication. if http is None: # Does the service require scopes? scopes = list( service.get('auth', {}).get('oauth2', {}).get('scopes', {}).keys()) # If so, then the we need to setup authentication if no developerKey is # specified. if scopes and not developerKey: # If the user didn't pass in credentials, attempt to acquire application # default credentials. if credentials is None: credentials = _auth.default_credentials() # The credentials need to be scoped. credentials = _auth.with_scopes(credentials, scopes) # Create an authorized http instance http = _auth.authorized_http(credentials) # If the service doesn't require scopes then there is no need for # authentication. else: http = build_http() if model is None: features = service.get('features', []) model = JsonModel('dataWrapper' in features) return Resource(http=http, baseUrl=base, model=model, developerKey=developerKey, requestBuilder=requestBuilder, resourceDesc=service, rootDesc=service, schema=schema)
def test_default_credentials(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) with self.assertRaises(ValueError): _auth.authorized_http(credentials)
def email_send(self, to, subject, templatePath, extraData=None, cc=None): """ to: recipient cc: python array, carbon copy recipients """ http = _auth.authorized_http(self.__mentor_cred) email_service = build('gmail', 'v1', http=http) personalizedPath = os.path.join("api", "emails", "templates", "placeholder.html") if cc is not None: cc = ','.join(cc) def updatePersonalizedHTML(templatePath, personalizedPath, extraData): """ Get HTML with the extraData filled in where specified. - Use Beautiful soup to find and replace the placeholder values with the proper user specific info - use 'with' to write the beautifulSoup string into a newFile - the personalized version of the original templatePath. This personalized version will be sent out in the email and will be rewritten everytime the function is called. """ with open(templatePath, 'r', encoding="utf8") as f: template = f.read() soup = BeautifulSoup(template, features="html.parser") if extraData != None: for key in extraData: target = soup.find_all(text=re.compile(r'%s' % key)) for v in target: v.replace_with(v.replace('%s' % key, extraData[key])) # now soup string has the proper values with open(personalizedPath, "w") as file: file.write(str(soup)) def create_message(to, subject, personalizedPath, cc=None): """Create a message for an email. Args: sender: Email address of the sender. to: Email address of the receiver. subject: The subject of the email message. personalizedPath: File path to email in html file with variable replaced with proper values. Returns: An object containing a base64url encoded email object. """ f = open(personalizedPath, 'r') message_text = f.read() sender = self.__mentor_cred._subject message = MIMEText(message_text, 'html') message['to'] = to message['cc'] = cc message['from'] = sender message['subject'] = subject # the message should converted from string to bytes. message_as_bytes = message.as_bytes() # encode in base64 (printable letters coding) message_as_base64 = base64.urlsafe_b64encode(message_as_bytes) raw = message_as_base64.decode() # need to JSON serializable return {'raw': raw} def send_message(service, user_id, message): """Send an email message. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. message: Message to be sent. Returns: Sent Message. """ try: message = (email_service.users().messages().send( userId=user_id, body=message).execute()) # print('Message Id: %s' % message['id']) return message except errors.HttpError as error: print('An error occurred: %s' % error) updatePersonalizedHTML(templatePath, personalizedPath, extraData) msg = create_message(to, subject, personalizedPath, cc) send_message(email_service, "me", msg)
def build_from_document( service, base=None, future=None, http=None, developerKey=None, model=None, requestBuilder=HttpRequest, credentials=None ): """Create a Resource for interacting with an API. Same as `build()`, but constructs the Resource object from a discovery document that is it given, as opposed to retrieving one over HTTP. Args: service: string or object, the JSON discovery document describing the API. The value passed in may either be the JSON string or the deserialized JSON. base: string, base URI for all HTTP requests, usually the discovery URI. This parameter is no longer used as rootUrl and servicePath are included within the discovery document. (deprecated) future: string, discovery document with future capabilities (deprecated). http: httplib2.Http, An instance of httplib2.Http or something that acts like it that HTTP requests will be made through. developerKey: string, Key for controlling API usage, generated from the API Console. model: Model class instance that serializes and de-serializes requests and responses. requestBuilder: Takes an http request and packages it up to be executed. credentials: oauth2client.Credentials or google.auth.credentials.Credentials, credentials to be used for authentication. Returns: A Resource object with methods for interacting with the service. """ if http is not None and credentials is not None: raise ValueError('Arguments http and credentials are mutually exclusive.') if isinstance(service, six.string_types): service = json.loads(service) if 'rootUrl' not in service and (isinstance(http, (HttpMock, HttpMockSequence))): logger.error( "You are using HttpMock or HttpMockSequence without" + "having the service discovery doc in cache. Try calling " + "build() without mocking once first to populate the " + "cache." ) raise InvalidJsonError() base = urljoin(service['rootUrl'], service['servicePath']) schema = Schemas(service) # If the http client is not specified, then we must construct an http client # to make requests. If the service has scopes, then we also need to setup # authentication. if http is None: # Does the service require scopes? scopes = list(service.get('auth', {}).get('oauth2', {}).get('scopes', {}).keys()) # If so, then the we need to setup authentication if no developerKey is # specified. if scopes and not developerKey: # If the user didn't pass in credentials, attempt to acquire application # default credentials. if credentials is None: credentials = _auth.default_credentials() # The credentials need to be scoped. credentials = _auth.with_scopes(credentials, scopes) # Create an authorized http instance http = _auth.authorized_http(credentials) # If the service doesn't require scopes then there is no need for # authentication. else: http = build_http() if model is None: features = service.get('features', []) model = JsonModel('dataWrapper' in features) return Resource( http=http, baseUrl=base, model=model, developerKey=developerKey, requestBuilder=requestBuilder, resourceDesc=service, rootDesc=service, schema=schema )
def update_meta_sheet_feeders(self, puzzle_id) -> None: """ Updates the input meta puzzle's spreadsheet with the latest feeder puzzle info """ meta_puzzle = Puzzle.objects.get(pk=puzzle_id) if not meta_puzzle.is_meta or not meta_puzzle.sheet: return logger.info("Starting updating the meta sheet for '%s' " "with feeder puzzles" % meta_puzzle) spreadsheet_id = extract_id_from_sheets_url(meta_puzzle.sheet) feeders = meta_puzzle.feeders.all() feeders = sorted(feeders, key=lambda p: p.name) feeders_to_answers = {f: f.correct_answers() for f in feeders} if len(feeders_to_answers) > 0: max_num_answers = max( 1, max((len(v) for v in feeders_to_answers.values()))) else: max_num_answers = 1 # Each thread needs its own http object because httplib2.Http() # is used under the hood and that is not thread safe. # Ref: https://github.com/googleapis/google-api-python-client/blob/master/docs/thread_safety.md http = _auth.authorized_http(self._credentials) sheets_service = self.sheets_service() response = (sheets_service.spreadsheets().get( spreadsheetId=spreadsheet_id, fields= "sheets.properties.title,sheets.properties.sheetId,sheets.protectedRanges", ).execute(http=http)) sheet_id = None protected_range_id = None for sheet in response["sheets"]: if sheet["properties"]["title"] == "AUTOGENERATED": sheet_id = sheet["properties"]["sheetId"] if "protectedRanges" in sheet and len( sheet["protectedRanges"]) > 0: protected_range_id = sheet["protectedRanges"][0][ "protectedRangeId"] break if sheet_id is None: # create AUTOGENERATED tab if it does not exist sheet_id = __add_sheet(sheets_service, http, spreadsheet_id, "AUTOGENERATED") else: # otherwise, clear the sheet __clear_sheet(sheets_service, http, spreadsheet_id, sheet_id) def __get_answer_or_blank(puzzle, i): if i < len(feeders_to_answers[puzzle]): return feeders_to_answers[puzzle][i] return "" requests = [ { "updateCells": { "fields": "userEnteredFormat.textFormat.fontFamily," "userEnteredValue.stringValue," "userEnteredValue.formulaValue," "userEnteredFormat.textFormat.bold", "range": { "sheetId": sheet_id, "startRowIndex": 0, "endRowIndex": 4 + len(feeders), "startColumnIndex": 0, "endColumnIndex": 3 + max_num_answers, }, "rows": [ { "values": [{ "userEnteredValue": { "stringValue": "THIS PAGE IS AUTOGENERATED AND WILL BE OVERWRITTEN WHEN THERE ARE PUZZLE UPDATES." } }] }, { "values": [{ "userEnteredValue": { "stringValue": "COPY INFO TO ANOTHER SHEET." } }] }, {}, { "values": [ { "userEnteredValue": { "stringValue": "Puzzle Link" }, "userEnteredFormat": { "textFormat": { "bold": True } }, }, { "userEnteredValue": { "stringValue": "Sheet" }, "userEnteredFormat": { "textFormat": { "bold": True } }, }, { "userEnteredValue": { "stringValue": "Puzzle Name" }, "userEnteredFormat": { "textFormat": { "bold": True } }, }, ] + [ { "userEnteredValue": { "stringValue": "Answer" }, "userEnteredFormat": { "textFormat": { "bold": True } }, }, ] * max_num_answers }, ] + [{ "values": [ { "userEnteredValue": { "formulaValue": '=HYPERLINK("%s", "puzzle")' % puzzle.url } }, { "userEnteredValue": { "formulaValue": '=HYPERLINK("%s", "sheet")' % puzzle.sheet } }, { "userEnteredValue": { "stringValue": puzzle.name } }, ] + [{ "userEnteredValue": { "stringValue": __get_answer_or_blank(puzzle, i) }, "userEnteredFormat": { "textFormat": { "fontFamily": "Roboto Mono" } }, } for i in range(max_num_answers)] } for puzzle in feeders], }, }, { "autoResizeDimensions": { "dimensions": { "sheetId": sheet_id, "dimension": "COLUMNS", "startIndex": 1, "endIndex": 3, }, }, }, ] if protected_range_id is None: requests.append({ "addProtectedRange": { "protectedRange": { "range": { "sheetId": sheet_id, }, "description": "Autogenerated page", "warningOnly": False, "editors": { "users": settings.GOOGLE_API_AUTHN_INFO["client_email"] }, } } }) sheets_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body={ "requests": requests }).execute(http=http) logger.info("Done updating the meta sheet for '%s' " "with feeder puzzles" % meta_puzzle)
def harvester(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) authorized_http = _auth.authorized_http(credentials) service = discovery.build('gmail', 'v1', http=authorized_http, cache_discovery=False) with mock.patch.object(self.harvester_class, 'get_credentials', return_value=service): return self.harvester_class(mock.MagicMock())
def build_from_document( service, base=None, future=None, http=None, developerKey=None, model=None, requestBuilder=HttpRequest, credentials=None, client_options=None, adc_cert_path=None, adc_key_path=None, ): if http is not None and credentials is not None: raise ValueError( "Arguments http and credentials are mutually exclusive.") if isinstance(service, six.string_types): service = json.loads(service) elif isinstance(service, six.binary_type): service = json.loads(service.decode("utf-8")) if "rootUrl" not in service and isinstance(http, (HttpMock, HttpMockSequence)): logger.error( "You are using HttpMock or HttpMockSequence without" + "having the service discovery doc in cache. Try calling " + "build() without mocking once first to populate the " + "cache.") raise InvalidJsonError() base = urljoin(service["rootUrl"], service["servicePath"]) if client_options: if isinstance(client_options, six.moves.collections_abc.Mapping): client_options = google.api_core.client_options.from_dict( client_options) if client_options.api_endpoint: base = client_options.api_endpoint schema = Schemas(service) if http is None: scopes = list( service.get("auth", {}).get("oauth2", {}).get("scopes", {}).keys()) if scopes and not developerKey: if credentials is None: credentials = _auth.default_credentials() credentials = _auth.with_scopes(credentials, scopes) if credentials: http = _auth.authorized_http(credentials) else: http = build_http() client_cert_to_use = None if client_options and client_options.client_cert_source: raise MutualTLSChannelError( "ClientOptions.client_cert_source is not supported, please use ClientOptions.client_encrypted_cert_source." ) if (client_options and hasattr(client_options, "client_encrypted_cert_source") and client_options.client_encrypted_cert_source): client_cert_to_use = client_options.client_encrypted_cert_source elif adc_cert_path and adc_key_path and mtls.has_default_client_cert_source( ): client_cert_to_use = mtls.default_client_encrypted_cert_source( adc_cert_path, adc_key_path) if client_cert_to_use: cert_path, key_path, passphrase = client_cert_to_use() http_channel = (http.http if google_auth_httplib2 and isinstance( http, google_auth_httplib2.AuthorizedHttp) else http) http_channel.add_certificate(key_path, cert_path, "", passphrase) if "mtlsRootUrl" in service and (not client_options or not client_options.api_endpoint): mtls_endpoint = urljoin(service["mtlsRootUrl"], service["servicePath"]) use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") if not use_mtls_env in ("never", "auto", "always"): raise MutualTLSChannelError( "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" ) if use_mtls_env == "always" or (use_mtls_env == "auto" and client_cert_to_use): base = mtls_endpoint if model is None: features = service.get("features", []) model = JsonModel("dataWrapper" in features) return { 'http': http, 'baseUrl': base, 'model': model, 'developerKey': developerKey, 'requestBuilder': requestBuilder, 'resourceDesc': service, 'rootDesc': service, 'schema': schema, }
def test_default_credentials(self): credentials = mock.Mock(spec=google.auth.credentials.Credentials) with self.assertRaises(ValueError): _auth.authorized_http(credentials)