Angelo DeLuca
Angelo DeLuca

Reputation: 55

Using Google Docs API to Insert Image From Google Drive

I'm wondering if it's possible to obtain the URI for an image located on one's Google Drive.

What I've Tried:

Drive API: GET https://www.googleapis.com/drive/v2/files/{fileId}

(Using the Google Docs batchUpdate ReplaceImageRequest to replace images)

uri = file["selfLink"] => "Invalid requests[0].replaceImage: Access to the provided image was forbidden."

uri = file["webContentLink"] => "Invalid requests[0].replaceImage: There was a problem retrieving the image. The provided image should be publicly accessible, within size limit, and in supported formats."

uri = file["alternateLink"] => (see above)

uri = file["embedLink"] => (see file["webContentLink"])

I was trying these properties– expecting the Docs API to automatically interpret drive links. But this was probably just wishful thinking; as it of course, did not work.

Upvotes: 3

Views: 2536

Answers (1)

Tanaike
Tanaike

Reputation: 201428

I believe your goal as follows.

  • You want to replace the image using the method of documents.batchUpdate in Google Docs API.
  • You want to use the image file on your Google Drive for this.

Issue and solution:

When the image is put to the Google Document using Docs API, it seems that the URL of the image is the external URL. So in your case, you can use the URL of webContentLink. But the file is required to be publicly shared as the viewer with "Anyone with the link". I think that the reason for your issue is this. So please do the following flow.

  1. Please publicly share the image file as the viewer with "Anyone with the link".
  2. Please request the batchUpdate using the URL of webContentLink of the shared image.

Note:

  • This answer supposes that you have already been able to get and put values for Google Document using Docs API. So please be careful about this.
  • Unfortunately, I couldn't understand your actual request body. When you modified like above, when another error occurs, can you provide the detail of your request body? By this, I would like to confirm it.

References:

Upvotes: 3

Related Questions