简体   繁体   English

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

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

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.私人链接- Google 幻灯片存在但不可共享。

Invalid Link - No existing Google Slides or wrong URL.无效链接- 没有现有的 Google 幻灯片或错误的 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): 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与此返回(presentationId 来自私有 Google 幻灯片 ID 还是虚构 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?我还有其他方法可以区分链接吗?

Thank you:D谢谢:D

Answer:回答:

You can't tell just from the link if the ID is invalid.您无法仅通过链接判断 ID 是否无效。 You'll have to actually make the request and handle how your app responds based on the HTTP status code.您必须根据 HTTP 状态代码实际发出请求并处理您的应用程序如何响应。

More Information:更多信息:

From the Google Sheets API documentation on presentation IDs :来自Google 表格 API 文档中有关演示文稿 ID的信息:

The presentation ID is a string containing letters, numbers, and some special characters.演示文稿 ID 是一个包含字母、数字和一些特殊字符的字符串。 The following regular expression can be used to extract the presentation ID from a Google Sheets [sic] URL:以下正则表达式可用于从 Google 表格 [原文] URL 中提取演示文稿 ID:

/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.没有文档解释这些是如何生成的,因此如果不拨打电话,就无法发现任何给定的 ID 是否有效。

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.根据经验,Google 幻灯片/文档/表格 ID 的长度为 44 个字符,并且如上面链接的文档中所述,将匹配特定的正则表达式。 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.有了这个,您可以对 ID 的有效性进行一些检查,但除此之外,必须进行调用以确保确定。

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"

I hope this is helpful to you!我希望这对你有帮助!

References:参考:


Related Questions:相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM