无法使用Google API提取假期

时间:2019-02-17 16:24:24

标签: google-api google-calendar-api

作为我正在开发的应用程序的一部分,我想提供一个假期清单。我正在尝试使用Google API来执行此操作。在编写代码之前,我只想看看返回的JSON是什么样的。

我在Google中设置了一个帐户,获得了一个API密钥,并设置为可以使用Google Calendar API。我对获取假期列表所需的HTML进行了研究,但似乎无法使其正常工作。我浏览了所有Calendar API文档,但未找到有关获取假期列表的任何信息。这是我尝试过的一些URL,以及收到的响应。我在网上(主要是在这里)找到了所有这些URL示例。我已经删除了我的API密钥。

https://www.googleapis.com/calendar/v3/calendars/en.uk%40holiday.calendar.google.com/events?key=myKey

{
    "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "notFound",
            "message": "Not Found"
        }
        ],
        "code": 404,
        "message": "Not Found"
    }
}

https://www.googleapis.com/calendar/v3/calendars/en.sa%40holiday@group.v.calendar.google.com/events?key=myKey

{
    "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "notFound",
            "message": "Not Found"
        }
        ],
        "code": 404,
        "message": "Not Found"
    }
}

https://www.googleapis.com/calendar/v3/calendars/en.uk%40holiday.calendar.google.com/events?key=myKey

{
    "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "notFound",
            "message": "Not Found"
        }
        ],
        "code": 404,
        "message": "Not Found"
    }
}

https://www.googleapis.com/calendar/v3/calendars/en.uk@holiday.calendar.google.com/events?key=myKey

{
    "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "notFound",
            "message": "Not Found"
        }
        ],
        "code": 404,
        "message": "Not Found"
    }
}

https://www.googleapis.com/calendar/v3/calendars/en.usa#holiday@group.v.calendar.google.com/events?key=myKey

{
    "error": {
        "errors": [
        {
            "domain": "global",
            "reason": "required",
            "message": "Login Required",
            "locationType": "header",
            "location": "Authorization"
        }
        ],
        "code": 401,
        "message": "Login Required"
    }
}

Google看到了我的请求,但都是错误。

enter image description here

似乎我在网上找到的URL一次可以使用,但似乎不再可用。

如何使用此功能?

更新:我尝试了以下答案中的信息,现在我明白了:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

我在Events:List API参考的“尝试使用此API”部分中尝试了en.danish#holiday@group.v.calendar.google.com,并且能够获取日期的JSON。我不明白为什么它可以在Try This API中使用,但不能通过url使用。我尝试过的最新网址:

https://www.googleapis.com/calendar/v3/calendars/en.danish#holiday@group.v.calendar.google.com/events?key=myKey

3 个答案:

答案 0 :(得分:0)

目前api中似乎有错误。公开的api应该使用API​​密钥进行访问,它们不再需要身份验证

bug report here

错误验证信息

GET https://www.googleapis.com/calendar/v3/calendars/en.danish%23holiday%40group.v.calendar.google.com?key={YOUR_API_KEY}

响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

如果您使用Oauth2并对自己进行身份验证,则会得到

{

 "kind": "calendar#calendar",
 "etag": "\"2IG_UfzQLHbZ3_DsNnNPPvIpYxU/iF_yglNpjvUgQba3YfGrk5JFgq4\"",
 "id": "en.danish#holiday@group.v.calendar.google.com",
 "summary": "Holidays in Denmark",
 "timeZone": "Europe/Copenhagen",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "eventHangout"
  ]
 }
}

答案 1 :(得分:0)

我能够通过对公共日历ID中的#@进行网址编码来解决此问题。

就我而言:

en.new_zealand#holiday@group.v.calendar.google.com

成为:

en.new_zealand%23holiday%40group.v.calendar.google.com/events

在我这样做之前,我一直收到401错误。

答案 2 :(得分:0)

对我来说,它有效,但是没有该国语言。

https://www.googleapis.com/calendar/v3/calendars/uk%40holiday.calendar.google.com/events?key=YOURKEY

这对像我这样的非英语国家来说很烦。