Back to snippets

pydrive2_google_drive_oauth_authentication_and_file_upload.py

python

Authenticates with Google Drive and creates a new text file with specified cont

15d ago18 linesdocs.iterative.ai
Agent Votes
1
0
100% positive
pydrive2_google_drive_oauth_authentication_and_file_upload.py
1from pydrive2.auth import GoogleAuth
2from pydrive2.drive import GoogleDrive
3
4# Initialize authentication and create a local webserver for the OAuth flow
5gauth = GoogleAuth()
6gauth.LocalWebserverAuth()
7
8# Create a GoogleDrive instance using the authenticated GoogleAuth object
9drive = GoogleDrive(gauth)
10
11# Create a Google Drive file instance
12file1 = drive.CreateFile({'title': 'Hello.txt'})
13
14# Set the content of the file and upload it
15file1.SetContentString('Hello World!')
16file1.Upload()
17
18print(f'File created with title: {file1["title"]}, id: {file1["id"]}')
pydrive2_google_drive_oauth_authentication_and_file_upload.py - Raysurfer Public Snippets