Example #1
0
 def _get_creds(
     self,
     credentials="credentials.json",
     token="gdrive.token",
     scopes=['https://www.googleapis.com/auth/drive'],
     headless=False,
 ):
     if Path(credentials).is_file():
         with open(credentials, "r") as credentials_stream:
             cred_json = json_reader(credentials_stream)
         creds = None
         if Path(token).is_file():
             with open(token, "r") as token_stream:
                 creds = self._json_to_cred(
                     token_stream, cred_json["installed"]["client_id"],
                     cred_json["installed"]["client_secret"])
         if not creds or not creds.valid:
             if creds and creds.expired and creds.refresh_token:
                 creds.refresh(Request())
             else:
                 flow = InstalledAppFlow.from_client_secrets_file(
                     credentials,
                     scopes,
                 )
                 if headless:
                     creds = flow.run_console()
                 else:
                     creds = flow.run_local_server(port=0)
             with open(token, "w") as token_stream:
                 json_writer(
                     self._cred_to_json(creds),
                     token_stream,
                     indent=2,
                 )
         return creds
Example #2
0
 def _json_to_cred(self, json_stream, client_id, client_secret):
     cred_json = json_reader(json_stream)
     creds = Credentials(cred_json["access_token"],
                         refresh_token=cred_json["refresh_token"],
                         token_uri="https://oauth2.googleapis.com/token",
                         client_id=client_id,
                         client_secret=client_secret)
     return creds
Example #3
0
 def read_index(self, index_path):
     """Reads index file and updates the index for the instance."""
     pathlib_index = Path(index_path)
     if pathlib_index.exists() and pathlib_index.is_file():
         with open(pathlib_index, "r") as index_fp:
             try:
                 file_json = json_reader(index_fp)
                 if "files" in file_json:
                     for file_entry in file_json["files"]:
                         if file_entry not in self.index["files"]:
                             self.index["files"].append(file_entry)
             except JSONDecodeError:
                 print(
                     f"WARNING: {pathlib_index} is not a valid JSON file.")