Search code examples
pythongoogle-docsgoogle-docs-api

File does not appear in Google Drive - Google docs created with Python


Code shown hereafter runs fine. I see the file being created in my Jupiter notebook so the script seems to work. Only problem is, the file does not appear in my Drive.

creds_1 = ServiceAccountCredentials.from_json_keyfile_name("creds_1.json", scope)
service = build('docs', 'v1', credentials=creds_1)
title = 'My Document'
body = {
  "body": {},
  "title": "My document2"
}
doc = service.documents().create(body=body).execute()
print('Created document with title: {0}'.format(doc.get('title')))

Solution

  • It's because the Service Account has it's own Drive, so you are not uploading it to your personal Drive. The Service Account is separate from your user.

    An option would be to create a folder in your Drive, share it with the Service Account and make the code upload the files there.

    Another method, if you are a G Suite User, would be configuring the Service Account to impersonate your user with Domain Wide Delegation and upload the files directly to your Drive.