Search code examples
google-drive-apigoogle-docs-api

How to get the url to Google doc image


I have a Google Docs document which contains one image. I found images's objectId as stated here https://developers.google.com/docs/api/reference/rest/v1/InlineObject but I can't understand how to get an url to that image. I tried searching for this objectId in Drive but it returns File not found.

Any ideas ?

do

Update

As noted by @Tanaike the image info is contained in the result.inlineObjects and not directly in the paragraph.

enter image description here


Solution

    • You want to retrieve the URL of the inserted image in Google Document using Google Docs API.

    If my understanding is correct, how about this answer?

    I think that the property of inlineObjectElement that you are checking is in the paragraph. The information of the inserted images can be seen at the property of inlineObjects. And the URL can be seen at the property of ImageProperties.

    The endpoint is as follows.

    Endpoint:

    GET https://docs.googleapis.com/v1/documents/{documentId}?fields=inlineObjects
    
    • Here, as a sample, inlineObjects is used as fields. You can also use * as fields.

    Result:

    The URL can be retrieved as follows. It supposes that response is the returned value from above endpoint.

    url = response.inlineObjects["kix.###"].inlineObjectProperties.embeddedObject.imageProperties.sourceUri
    
    • kix.### is inlineObjectId in your question.
    • If the image is inserted from the URL of outside, the URL is the same with the URL which was used when the image was inserted.
    • If the image is inserted from Google Drive, the URL is like below.
      • https://lh3.google.com/u/0/d/{fileId}=w###-h###
      • In this case, {fileId} is the file ID of the image. You can retrieve the original file using this file ID.

    References:

    If I misunderstood your question and this was not the result you want, I apologize.