未配置 Google Calendar API v3 访问权限 [英] Google Calendar API v3 Access Not Configured

查看:26
本文介绍了未配置 Google Calendar API v3 访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Google 的 API v3 从客户的公共日历中获取事件列表.我在 ;但是数据很混乱,我认为代表了旧版本的 API.经过一番搜索,我找到了 v3 API 的正确 URL 结构:https://www.googleapis.com/calendar/v3/calendars/dl9fj86o2ohe7o823s7jar920s%40group.calendar.google.com/events?key={API_KEY}.

但是就像这个问题,我得到了一个错误.执行以下操作:

1.创建项目

通过转至 Google 开发者控制台并点击创建项目来实现.我对此感到困惑,因为我的应用程序完全是前端的,而且我认为我不需要 Google Developer 项目.我错了;我需要一个项目来执行后续步骤.

2.为您的项目创建 API 密钥

创建项目后,点击项目名称并导航到APIs &身份验证 > 凭据.在公共API访问"下,点击Create new key > {KEY_TYPE} > Create;在我的例子中 {KEY_TYPE}Browser key 因为我有一个完全前端的应用程序.现在跳过填写推荐人.这应该会为您创建一个 API 密钥,您可以将其插入到上面的 URL 中(上面写着 {API_KEY}.

3.添加引用

如果您已经做到了这一点,您应该会看到 OP 所说的错误.您收到此错误的原因是,即使日历是公开的,Google 也只允许来自指定域的请求.因此,我可以发布我的日历 ID 甚至我的 API 密钥,如果没有我的允许,其他开发人员将无法以编程方式访问我的日历.

要解决此问题,请点击APIs & 下的Edit allowed referersauth > Credentials—并添加 (1) 将向 API 发出请求的域的名称和 (2) 如果您正在本地开发 http://localhost:{PORT}/*.确保在末尾添加通配符.

4.从允许的域发出 HTTP 请求

在完成所有这些配置之后,如果您只是将 URL 粘贴到浏览器中,您仍然会收到错误消息.这是因为请求必须来自您刚刚允许的域之一.只需从您正在构建的任何应用程序发出请求.就我而言,JavaScript (jQuery) 如下所示:

$.ajax({类型:'获取',网址:{MY_URL},成功:功能(数据){//在这里抛出一个调试器语句,//你应该能够检查你的数据.}});

I'm trying to get a list of events from a client's public calendar using v3 of Google's API. I entered the Calendar ID into the API Explorer, and I'm getting a positive result:

https://www.googleapis.com/calendar/v3/calendars/rpsj44u6koirtq5hehkt21qs6k%40group.calendar.google.com/events?key={YOUR_API_KEY}`

=> [List of events here, as expected]

To create an API key, I created a project in the Google Developer Console, created a Public API access key (APIs & auth > Credentials), and replaced {YOUR_API_KEY} above with my actual key. I made sure that the Calendar API was turned on (APIs & auth > APIs). When I paste this URL in the browser, I get this error response:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration.",
    "extendedHelp": "https://console.developers.google.com"
   }
  ],
  "code": 403,
  "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration."
 }
}

All the responses I've seen say that you need to make sure the Google Calendar API is turned on, and it definitely is (also, it's turned on by default). What am I missing here?

解决方案

Using Google's Calendar API frustrated me for a couple of hours, and I want to document an overly complete (beyond the scope of this question) answer from a few sources for anyone else who may be having difficulty.

First, if you're like me, you got the public address from your calendar's settings:

For my public calendar, that XML link was this; but the data was messy and, I think, represented an older version of the API. After searching around a bit, I found the correct URL structure for the v3 API: https://www.googleapis.com/calendar/v3/calendars/dl9fj86o2ohe7o823s7jar920s%40group.calendar.google.com/events?key={API_KEY}.

But like this question, I was getting an error. Do the following:

1. Create a project

Do that by going to the Google Developer Console and clicking Create Project. I was confused by this because my application is entirely front-end, and I didn't think I needed Google Developer project. I was wrong; I needed a project to perform the next steps.

2. Create an API key for your project

After creating the project, click on the project name and navigate to APIs & auth > Credentials. Under "Public API access", click Create new key > {KEY_TYPE} > Create; in my case {KEY_TYPE} was Browser key since I have an entirely front-end application. Skip filling in referers for now. This should create you an API key that you insert into the URL above (where it says {API_KEY}.

3. Add referers

If you've made it this far, you should see the error OP was talking about. The reason you get this error is that even though the calendar is public, Google only allows requests from specified domains. So I could publish my calendar's ID and even my API key, and another developer would not be able to access my calendar programmatically without me allowing that.

To resolve this, click Edit allowed referers—under APIs & auth > Credentials—and add (1) the name of the domain that will be making the request to the API and (2) if you're developing locally http://localhost:{PORT}/*. Make sure you add the wildcard at the end.

4. Make an HTTP request from an allowed domain

After all of this configuration, you'll still get an error if you just paste the URL into your browser. This is because the request has to come from one of the domains you just allowed. Just make the request from whatever application you're building. In my case, the JavaScript (jQuery) looks like this:

$.ajax({
    type: 'GET',
    url: {MY_URL},
    success: function(data) {
        // Throw a debugger statement in here,
        // and you should be able to inspect your data.
    }
});

这篇关于未配置 Google Calendar API v3 访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆