Created
May 3, 2024 16:48
-
-
Save Darkflib/1fbb580caa93d7861c527956708f25a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleapiclient.discovery import build | |
from google.oauth2 import service_account | |
def create_and_share_google_doc(service_account_file, user_email): | |
# Authenticate with the service account | |
creds = service_account.Credentials.from_service_account_file(service_account_file, scopes=[ | |
'https://www.googleapis.com/auth/documents', | |
'https://www.googleapis.com/auth/drive' | |
]) | |
# Create the Docs and Drive service | |
docs_service = build('docs', 'v1', credentials=creds) | |
drive_service = build('drive', 'v3', credentials=creds) | |
# Create a new document | |
document = docs_service.documents().create(body={}).execute() | |
document_id = document['documentId'] | |
# Share the document using the Drive API | |
drive_service.permissions().create( | |
fileId=document_id, | |
body={ | |
'type': 'user', | |
'role': 'writer', | |
'emailAddress': user_email | |
}, | |
fields='id' | |
).execute() | |
print(f"Document created and shared. Document ID: {document_id}") | |
# Call the function with appropriate parameters | |
create_and_share_google_doc('path_to_service_account.json', 'user@example.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment