[Python] Google Docs API - 403 - googleapiclient.errors.HttpError: 403

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    Problem: I am facing this error-

    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "The caller does not have permission". Details: "The caller does not have permission">

    Traceback (most recent call last):
    File "/app/book_editor/nodes.py", line 554, in create_and_share_book_doc
    doc = docs_service.documents().create(body={"title": book.book_title}).execute()
    File "/usr/local/lib/python3.10/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
    File "/usr/local/lib/python3.10/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "The caller does not have permission". Details: "The caller does not have permission">


    Background: I am using google docs api to create docs, and add some content into it. I have enabled the Google Docs API, deployed on GCP Cloud Run using docker image, with a compute engine service account. I also added path to credentials.json file in ENV variable for GOOGLE_APPLICATION_CREDENTIALS in docker container. i checked that google service account email for cloud run service and credentials.json file are same.

    Question: how to assign Docs API Owner or Editor role using IAM to this service account?

    My code:

    scope = [
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/drive",
    "https://spreadsheets.google.com/feeds",
    "https://www.googleapis.com/auth/drive.file",
    ]
    creds = ServiceAccountCredentials.from_json_keyfile_name("config/gkey.json", scope)
    docs_service = build("docs", "v1", credentials=creds, cache_discovery=False)
    drive_service = build("drive", "v3", credentials=creds, cache_discovery=False)

    doc = docs_service.documents().create(body={"title": book.book_title}).execute()


    I expect it to run smoothly, create a new document and fill the content.

    Continue reading...

Compartilhe esta Página