【问题标题】:Using Google Calendar API returns 401 (unauthorized)使用 Google Calendar API 返回 401(未经授权)
【发布时间】:2014-10-22 21:40:43
【问题描述】:

我正在尝试通过 JavaScript 使用 Google 日历 API,并在使用以下代码时不断收到 401 错误响应:

$.ajax({ 
   dataType: "json", 
   url: "https://www.googleapis.com/calendar/v3/users/me/calendarList/<calendarID>?key=<my api key>", 
   success: function(result){ 
   $('#result').html(result); 
   } 
});

我使用 API 有什么问题吗?我应该如何使用oAuth?我已经有来自 Google 控制台的 clientIDsecretID,但不知道如何使用它们。

谢谢。

【问题讨论】:

  • 我认为您不能像这样简单地调用该 API,因为 Google 希望用户手动授权访问。将您的网址粘贴到浏览器中,看看会发生什么。
  • 我知道我可能在身份验证方面遗漏了一些东西。粘贴 url,我得到以下信息: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": “标题”,“位置”:“授权”}],“代码”:401,“消息”:“需要登录”}}
  • 此 API 的工作方式是,首先您使用 Google API 提供的不同 url 并传入您的 api 密钥并返回 url。然后该网址首先重定向到登录页面,然后是授权页面。在返回 url 中,您会获得一个令牌,您可以在 REST api 中使用它来检索数据。查看 Google 文档。
  • 我正在使用由 google 提供的此工具生成的 url,developers.google.com/google-apps/calendar/v3/reference/… 在那里,您输入您的 ID,它会为您获取 URL。我检查了 google 文档并已在其中这几天,你推荐某个页面再读一遍吗?谢谢!

标签: javascript json api google-calendar-api http-status-code-401


【解决方案1】:

这是经过大量研究后的答案,我刚刚找到并使用了本指南来使用 google calendar api http://googleappsdeveloper.blogspot.com/2011/12/using-new-js-library-to-unlock-power-of.html

然后我仍然感到困惑,因为每次尝试重新生成 api 密钥都无法正常工作几次。然后我不得不使用没有任何推荐人的浏览器密钥,就像这里提到的那样:Getting Error 403: Access Not Configured. Please use Google Developers Console to activate the API for your project

最后我让一切正常了。

这里也是了解 OAuth2 的主要文章 https://developers.google.com/accounts/docs/OAuth2

【讨论】:

    【解决方案2】:

    为了使用 Google Calendar API,您不仅需要客户端 ID 和客户端密码,而且在大多数情况下(除了访问公共日历),您还需要对用户进行身份验证。实现这一目标的一种方法是使用 Oauth2。

    我推荐使用可以在这里下载的客户端库:https://developers.google.com/google-apps/calendar/downloads

    这里还有一个关于如何使用该库的示例:https://developers.google.com/api-client-library/javascript/samples/samples

    【讨论】:

    • 感谢您的文章,我仍然不清楚它得到 403 错误。我刚刚发布了答案。
    猜你喜欢
    • 2018-10-15
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多