Home > Software design >  Google API is not creating any document in the drive when I'm trying to create a blank document
Google API is not creating any document in the drive when I'm trying to create a blank document

Time:12-16

I'm trying the below code to generate the google doc

    SERVICE_FILENAME = 'C:/Users/XYZ/Test/service_account.json'  # set path to service account filename
    
    from googleapiclient.discovery import build
    from google.oauth2 import service_account
    
    from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
    
    credentials = service_account.Credentials.from_service_account_file(SERVICE_FILENAME,
                                                                        scopes=['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/documents']
                                                                        )
    
    # drive = build('drive', 'v3', credentials=credentials)
    drive = build('docs', 'v1', credentials=credentials)
    # file_metadata = {'name': filepath,
    #                  'mimeType': 'application/vnd.google-apps.document'}

    # media = MediaFileUpload(filepath,
    #                          mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
    # file = drive.files().create(body=file_metadata,
    #                             # media_body=media,
    #                             fields='id').execute()
    file_metadata = {
        "title": "xyz",
        "body": {}
    }

    file = drive.documents().create(body=file_metadata).execute()
    print('File ID: %s' % file.get('id'))
    

But I'm not getting any file id and no file is getting created in the google doc. It says File ID: None

First, tried with drive API but that didn't work then I went for doc API which is also not working. Note: Both the APIs are enabled from GCP.

CodePudding user response:

In your situation, how about the following modification?

From:

print('File ID: %s' % file.get('id'))

To:

print('File ID: %s' % file.get('documentId'))
  • You can retrieve the document ID of the created Google Document by file.get('documentId').

References:

CodePudding user response:

As an addition of what @Tanaike says, service accounts do not belong to your Google WorkSpace domain. The files created by a service account are not created in your Google Workspace Domain. If you want more information on how Service Accounts work versus regular accounts, you can check the Documentation.

As a workaround, you could share the file with a user, group, domain, etc. Adapted from Share file example :

create_share_sa.py
def uploadFiles():
    drive = build_service('drive', 'v3')
    new_file = drive.files().create(body={"name": "Testing"}).execute()
    # User share
    new_perm_user = {
        "type": "user",
        "role": "owner",
        "emailAddress": "[email protected]",
    }
    # Domain Share
    # new_perm = {
    #     "type": "domain",
    #     "role": "reader",
    #     "domain": "domain",
    #     "allowFileDiscovery": True
    # }
    perm_id = drive.permissions().create(fileId=new_file['id'],
                                         body=new_perm_user).execute()
Documentation
  • Related