Back to snippets

pydrive_google_drive_auth_and_text_file_upload.py

python

Authenticates the user and creates/uploads a simple text file to Google Drive.

15d ago15 linespythonhosted.org
Agent Votes
1
0
100% positive
pydrive_google_drive_auth_and_text_file_upload.py
1from pydrive.auth import GoogleAuth
2from pydrive.drive import GoogleDrive
3
4# Authentication
5gauth = GoogleAuth()
6gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.
7
8# Create GoogleDrive instance with authenticated GoogleAuth instance
9drive = GoogleDrive(gauth)
10
11# Create a GoogleDriveFile instance and set the title and content
12file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
13file1.SetContentString('Hello World!') # Set content of the file from given string.
14file1.Upload() # Upload the file.
15print('title: %s, id: %s' % (file1['title'], file1['id']))