Using Google Docs API to Insert Image From Google Drive - google-drive-api

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.

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.
Please publicly share the image file as the viewer with "Anyone with the link".
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:
Method: documents.batchUpdate
ReplaceImageRequest

Related

Using ApI is it possible to edit or open Google Sheet?

Using API to get access_token and now I need to open Google Sheet.
Step 1.
Authorization: Bearer ya29.a0AV...." together with link: "https://docs.google.com/spreadsheets/u/0/d/[DocumentFileID]/edit"
Step 2.
Now HTML document back (~200kb). I'm using libcurl for that.
The problem is - missing are some css and js files and I see document just partially. Is there any way to open/edit Google Sheet using API?
I know for the option to just open https://docs.google.com/spreadsheets/u/0/d/[DocumentFileID]/edit in the new browser tab, but in that case I need to manually login to Google and that's what I try to avoid, because I want to use API to login and to open/edit a Sheet.
I also know for the option to share a document but I also try to avoid that. I'm writing application to customers who can access they own documents once they confirmed API usage.
I think you may have misunderstood what the google sheets api can do
The Google Sheets API is a RESTful interface that lets you read and modify a spreadsheet's data.
It gives you access to the data, it does not allow you to open the google sheets web application. Your going to have to code your own google sheets type app if you want users to open sheets directly.

Unable to get the ID of opened .docx file in Docs Add-on

I am stuck at getting the ID of the .docx file inside Google Docs Add-on.
The Id is present in the URL of the browser but I am unable to find any method to get the browser URL.
The method DocumentApp.getActiveDocument().getId() is not helping because it works on Google Docs file, and the file that I open in Google doc is Microsoft Word Format (.docx) file.
I am attaching a screenshot for reference: Reference image
Is there any way to get the ID of .docx file inside Google Docs Add-on?
Or is there any way to get the URL of the browser when the .docx is opened in the Google Docs Add-on?
Thanks in advance.
Answer:
There's no way to retrieve this information for non-Google Docs in Workspace add-ons.
Explanation:
As you mentioned, DocumentApp methods only work for Google Docs files.
Also, the Workspace add-on's Docs event object is only populated if the current document is a Google Docs. I just checked that's the case.
Finally, this information is not retrievable in the context of Workspace add-ons.
Feature request:
You could consider filing a feature request using this Issue Tracker template if you think this functionality could be useful.
Check out the event object for Workspace Addons using this link. When you build your card, you have access to the event. So you should be able to access it using docs.id within your function.

Within the google docs API, Can I insert a picture from my computer without being from a webpage?

I am trying to use screenshots that I receive from a python script using selenium to be able to add it or send it through to a google doc API to be able to add it to a google apps script.
Is this a possible using the google docs script api?
I found this example that I think may work, but I am not sure if an online picture is needed or not.
https://developers.google.com/docs/api/how-tos/images
TLDR:
Can the api call a local .png or does it have to be a http:// like the example?

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 ?
Update
As noted by #Tanaike the image info is contained in the result.inlineObjects and
not directly in the paragraph.
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:
Method: documents.get
InlineObject
ImageProperties
If I misunderstood your question and this was not the result you want, I apologize.
Step #1: Right-click and Copy the image from the Google Doc.
Step #2: Paste into dynalist.io.
(Note: Dynalist doesn't allow you to paste images, but you can paste images from Google Docs because it just adds the link.)
I understand that this may not help if you are trying to do something with the API, but for finding the link to a single image, it is the simplest method I've found.
use the drive and doc api
use the gdoc api retrieve the ContentUri:
(Document.InlineObjects[imageid].InlineObjectProperties.EmbeddedObject.ImageProperties.ContentUri)
imageid has the form "kix.###..."
The imageid can be retrieved frm the InlineObjects map, the key is the imageid
After obtaining the ContentUri you must strip the ending "=wxx-hyy" to obtain the the image file id for the drive api
use the drive api to obtain the file pointer:
*fil, err = service.Files.Get(fileid).Do()
After obtaining the file pointer, you can retrieve the image file characteristics (see File)
Alternatively the image file can be downloaded via:
httpResp, err =service.Files.Export(fileid, mime).Download()
mime types are listed here

edit link from google drive api

While I get a file object from google drive api, I want to get the link to edit this file with google docs interface.
But as defined here, I only get the link to google docs if the drive.apps.readonly scope is set to readonly.
Is there any way to get this link for editing?
You should actually use drive.apps.readonly scope. "readonly" means that you cannot edit installed google apps, but you still can get list of these apps, and open any document for editing.