Search code examples
jsongoogle-oauthgoogle-api-python-clientgoogle-docs-apipython-3.12

How to fix error in get doc from Google API?


from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
import random
import msvcrt 
import json

credentials_doc = 'service_account.json'


service_account_info = json.load(open(credentials_doc))
credentials = Credentials.from_service_account_info(service_account_info)
service = build('drive', 'v3', credentials=credentials)

document_id='my_doc_drive_id'

document = service.comments().get(documentId=document_id).execute()
print(document)

error: document = service.comments().get(documentId=document_id).execute() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ raise TypeError("Got an unexpected keyword argument {}".format(name)) TypeError: Got an unexpected keyword argument documentId

how can i get my document ** drivelink:https://docs.google.com/document/d/my_doc_drive_id**

I want to get my drive doc infos and processing incomings


Solution

  • From how can i get my document ** drivelink:https://docs.google.com/document/d/my_doc_drive_id** and I want to get my drive doc infos and processing incomings, I believe that your goal is as follows.

    • You want to retrieve a Google Document by googleapis for Python.
    • Your service account can access the Google Document of document_id='my_doc_drive_id'.

    Modification points:

    • Your script tries to retrieve the comment from a file using Drive API. Also, when the comment is retrieved, it is required to use fileId and commentId. From your question, I guess that your document_id='my_doc_drive_id' might be the Google Document ID which is not a comment ID. I guessed that this might be the reason for your current issue.

    If you want to retrieve the Google Document, it is required to use Google Docs API.

    Modified script:

    from google.oauth2.service_account import Credentials
    from googleapiclient.discovery import build
    import json
    
    credentials_doc = 'service_account.json'
    
    service_account_info = json.load(open(credentials_doc))
    credentials = Credentials.from_service_account_info(service_account_info)
    
    service = build('docs', 'v1', credentials=credentials)
    
    document_id='my_doc_drive_id' # Please set your Google Document ID.
    
    document = service.documents().get(documentId=document_id).execute()
    print(document)
    
    • When this script is run, when your service account can access the Document of document_id='my_doc_drive_id', the object from Google Document is retrieved.

    Note:

    • If you want to retrieve the file metadata of Google Documents using Drive API, I think that the following script can be used.

      from google.oauth2.service_account import Credentials
      from googleapiclient.discovery import build
      import json
      
      credentials_doc = 'service_account.json'
      
      service_account_info = json.load(open(credentials_doc))
      credentials = Credentials.from_service_account_info(service_account_info)
      
      service = build("drive", "v3", credentials=credentials)
      
      document_id='my_doc_drive_id' # Please set your Google Document ID.
      
      document = service.files().get(fileId=document_id, fields="*").execute()
      print(document)
      

    References: