|
| 1 | +# import os |
| 2 | + |
| 3 | +# from google_auth_oauthlib.flow import Flow |
| 4 | + |
| 5 | +# with open("temp_spreadsheet_creds.json", "w", encoding="utf-8") as tsc: |
| 6 | +# tsc.write(os.getenv("SPREADSHEET_CREDS", "")) |
| 7 | +# # Create the flow using the client secrets file from the Google API |
| 8 | +# # Console. |
| 9 | +# flow = Flow.from_client_secrets_file( |
| 10 | +# "temp_spreadsheet_creds.json", |
| 11 | +# scopes=["https://www.googleapis.com/auth/spreadsheets"], |
| 12 | +# redirect_uri="https://design-computing.github.io/", |
| 13 | +# ) |
| 14 | + |
| 15 | +# # Tell the user to go to the authorization URL. |
| 16 | +# auth_url, _ = flow.authorization_url(https://github.com/prompt="consent") |
| 17 | + |
| 18 | +# print(f"Please go to this URL:\n\n{auth_url}\n") |
| 19 | + |
| 20 | +# # The user will get an authorization code. This code is used to get the |
| 21 | +# # access token. |
| 22 | +# code = input("Enter the authorization code: ") |
| 23 | +# flow.fetch_token(code=code) |
| 24 | + |
| 25 | +# # You can use flow.credentials, or you can just get a requests session |
| 26 | +# # using flow.authorized_session. |
| 27 | +# session = flow.authorized_session() |
| 28 | +# print(session.get("https://www.googleapis.com/userinfo/v2/me").json()) |
| 29 | + |
| 30 | + |
| 31 | +import os |
| 32 | +import pprint |
| 33 | + |
| 34 | +import google.oauth2.credentials |
| 35 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 36 | +from googleapiclient.discovery import build |
| 37 | +from googleapiclient.errors import HttpError |
| 38 | + |
| 39 | +pp = pprint.PrettyPrinter(indent=2) |
| 40 | + |
| 41 | +# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains |
| 42 | +# the OAuth 2.0 information for this application, including its client_id and |
| 43 | +# client_secret. |
| 44 | +with open("temp_spreadsheet_creds.json", "w", encoding="utf-8") as tsc: |
| 45 | + tsc.write(os.getenv("SPREADSHEET_CREDS", "")) |
| 46 | +CLIENT_SECRETS_FILE = "temp_spreadsheet_creds.json" |
| 47 | + |
| 48 | +# This access scope grants read-only access to the authenticated user's Drive |
| 49 | +# account. |
| 50 | +SCOPES = ["https://www.googleapis.com/auth/spreadsheets"] |
| 51 | +API_SERVICE_NAME = "spreadsheets" |
| 52 | +API_VERSION = "v4" |
| 53 | + |
| 54 | + |
| 55 | +def get_authenticated_service(): |
| 56 | + flow = InstalledAppFlow.from_client_secrets_file( |
| 57 | + CLIENT_SECRETS_FILE, |
| 58 | + SCOPES, |
| 59 | + redirect_uri="https://design-computing.github.io/", |
| 60 | + ) |
| 61 | + credentials = flow.run_console() |
| 62 | + return build(API_SERVICE_NAME, API_VERSION, credentials=credentials) |
| 63 | + |
| 64 | + |
| 65 | +def list_drive_files(service, **kwargs): |
| 66 | + results = service.files().list(**kwargs).execute() |
| 67 | + |
| 68 | + pp.pprint(results) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + # When running locally, disable OAuthlib's HTTPs verification. When |
| 73 | + # running in production *do not* leave this option enabled. |
| 74 | + os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" |
| 75 | + service = get_authenticated_service() |
| 76 | + list_drive_files(service, orderBy="modifiedByMeTime desc", pageSize=5) |
0 commit comments