{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# !pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "import os\n", "import google.auth\n", "import google_auth_oauthlib.flow\n", "import googleapiclient.discovery\n", "import googleapiclient.errors\n", "from google.oauth2.credentials import Credentials" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=165594406980-5f6scupsune6ise7dqp04ti3mmt2eqe9.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A52470%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.readonly&state=TRbdRt2JQkFtZttqScUDD9N4DW7scP&access_type=offline\n" ] } ], "source": [ "# If modifying these SCOPES, delete the file token.json.\n", "SCOPES = ['https://www.googleapis.com/auth/drive.readonly']\n", "\n", "def main():\n", " \"\"\"Shows basic usage of the Drive v3 API.\n", " Prints the names and ids of the first 10 files the user has access to.\n", " \"\"\"\n", " creds = None\n", " # The file token.json stores the user's access and refresh tokens, and is\n", " # created automatically when the authorization flow completes for the first time.\n", " if os.path.exists('token.json'):\n", " creds = Credentials.from_authorized_user_file('token.json', SCOPES)\n", " # If there are no (valid) credentials available, let the user log in.\n", " if not creds or not creds.valid:\n", " if creds and creds.expired and creds.refresh_token:\n", " creds.refresh(google.auth.transport.requests.Request())\n", " else:\n", " flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(\n", " 'credentials.json', SCOPES)\n", " creds = flow.run_local_server(port=0)\n", " # Save the credentials for the next run\n", " with open('token.json', 'w') as token:\n", " token.write(creds.to_json())\n", "\n", " service = googleapiclient.discovery.build('drive', 'v3', credentials=creds)\n", "\n", " # Call the Drive v3 API\n", " results = service.files().list(\n", " pageSize=10, fields=\"nextPageToken, files(id, name)\").execute()\n", " items = results.get('files', [])\n", "\n", " if not items:\n", " print('No files found.')\n", " else:\n", " print('Files:')\n", " for item in items:\n", " print(u'{0} ({1})'.format(item['name'], item['id']))\n", "\n", "if __name__ == '__main__':\n", " main()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }