Google 幻灯片 Api - 检查 Google 幻灯片 URL 是否私密或无效

Google Slides Api - Check Google Slides URL if is in private or invalid

我有一个应用程序,用户可以输入 Google 幻灯片的 link,如果成功,它将 view/render 另一个组件上的 Google 幻灯片。但我也想通知用户,如果他们输入的 link 是 public,私有或无效 。所以基本上当它是 Public Link 时,它会成功显示 Google 幻灯片,但问题是我怎么知道 link 是 Private LinkInvalid Link?

Private Link - Google slide is existing but not shareable.

Invalid Link - No existing Google Slides or wrong URL.

我尝试将此 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 object400 与此 returns 一起使用(无论 presentationId 是来自私人 Google 幻灯片 ID 还是编造ID):

{
  "error": {
    "code": 400,
    "message": "This operation is not supported for this document",
    "status": "FAILED_PRECONDITION"
  }
}

还有其他方法可以区分 link 吗?

答案:

您无法仅从 link 判断 ID 是否无效。您必须实际发出请求并根据 HTTP 状态代码处理应用的响应方式。

更多信息:

来自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-_]+)

这个字符串本身就是一个字符串。没有文档解释这些是如何生成的,因此如果不进行调用就无法发现任何给定 ID 是否有效。

处理:

根据经验,Google Slides/Docs/Sheets ID 的长度为 44 个字符,如上文 linked 文档所述,将匹配特定的正则表达式。有了这个,您可以对 ID 的有效性进行 一些 检查,但除此之外,必须进行调用以确保确定。

一些伪代码让你继续:

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"

参考文献:


相关问题: