Kariamoss Kariamoss - 2 months ago 9
Python Question

Gather the webViewLink and webContentLink in Python

I'm trying to get the

webContentLink
and the
webViewLink
of a Google Document. For the moment, my code looks like this :

http = decorator.http()
service = discovery.build("drive", "v2", http=http)
fields = "items(id,title,mimeType,webViewLink,webContentLink),nextLink,nextPageToken"
list = service.files().list(fields=fields).execute(http)
docs = [docs for docs in list["items"]
if docs["mimeType"] == DOCS_MIMETYPE]
template_values = {
'docs': docs,
}


The
id
and the
title
of the document are here but the
webViewLink
and
webContentLink
are both empty.

How can I get them properly?

Answer

Be noted that the webViewLink property is only returned for public folders, and not the single files inside such folders. You can use that as the base url to construct links to your files.

To retrieve the WebViewLink property, your request should look like this:

results = service.files().list(
        pageSize=10,fields="nextPageToken, files(id, name, webViewLink)").execute() 

Check these related SO questions: