I have an app that the user can input the link of the Google Slides and it will view/render the Google Slide on another component if successful. But i also want to notify the user if the link they inputted was public, private or invalid. So basically when it’s a Public Link, it will show the Google Slides successfully, but the problem is how do I know if the link is a Private Link or an Invalid Link?
Private Link – Google slide is existing but not shareable.
Invalid Link – No existing Google Slides or wrong URL.
I tried using this API GET https://slides.googleapis.com/v1/presentations/{presentationId}, given that I already have the presentationId
, but I only got 2 responses, the 200
which returns successfully with this object and 400
which returns with this (whether the presentationId is from a private Google Slide ID or a made up ID):
{ "error": { "code": 400, "message": "This operation is not supported for this document", "status": "FAILED_PRECONDITION" } }
Is there another way for me to differentiate the link?
Advertisement
Answer
Answer:
You can’t tell just from the link if the ID is invalid. You’ll have to actually make the request and handle how your app responds based on the HTTP status code.
More Information:
From the Google Sheets API documentation on presentation IDs:
The presentation ID is a string containing letters, numbers, and some special characters. The following regular expression can be used to extract the presentation ID from a Google Sheets [sic] URL:
/presentation/d/([a-zA-Z0-9-_]+)
This string is as such – just a string. There is no documentation which explains how these are generated and so there is no way of discovering if any given ID is valid without making the call.
Handling:
From experience, Google Slides/Docs/Sheets IDs are 44 characters long, and as stated in the documentation linked above, will match a specific regular expression. With this, you can make some checks as to the validity of the ID, but other than this the call must be made to check for sure.
Some psuedo-code to get you going:
url = https://docs.google.com/presentation/d/some-presentation-id/edit slideId = url.extract("/presentation/d/([a-zA-Z0-9-_]+)") if slideId == null OR slideId.length != 44: return "Presentation URL not valid" else: request = makeHTTPRequest(url) if request.responseCode = 200: // display slide else if request.responseCode = 400: return "can't access this url, invalid or private"